Using Java to parse XML

后端 未结 7 769
忘了有多久
忘了有多久 2020-12-21 07:23

I have made a PHP script that parses an XML file. This is not easy to use and I wanted to implement it in Java.

Inside the first element there are various count of

7条回答
  •  心在旅途
    2020-12-21 07:44

    The easiest way, if performance is not a main concern, is probably XPath. With XPath, you can find nodes and attributes simply by specifying a path.

    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = xpath.compile();
    NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    

    The xpath_expression could be as simple as

    "string(//member/observedProperty/@href)"
    

    For more information about XPath, XPath Tutorial from W3Schools is pretty good.

提交回复
热议问题