简体   繁体   中英

Parse XML to retrieve multiple values of same tag

I am new to XPath and XML. I am trying to retrieve values of a particular tag from tag. That particular tag in following tree structure

<article>
<front>
<article-meta>
<supplementary-material id="SM2379">
<caption><title>arg_3.docx</title></caption>
</supplementary-material>
<supplementary-material id="SM2375">
<caption><title>arg_2.docx</title></caption>
</supplementary-material>
<supplementary-material id="SM2373">
<caption><title>Sulental_material.docSulental_material.docSulental_material.docSulental_material.docSulental_material.docSulental_material.docSulental_material.docSulental_material.docSulental_material.docSulental_material.docSulental_material.docSulental_material.docSulental_material.docSulental_material.docSulental_material.docSulental_material.docSulental_material.docSulental_material.doc</title></caption>
</supplementary-material>
</article-meta>
</front>
</article>

I want to retrieve all ' title ' tag in this sample file along with ' id ' of parent tag. As I need to add to existing functionality I have some limitation like use jdom, can use xpath etc

Any help will be truly appreciated

It is quite easy to achieve what you're trying to do if the nodes are built up straight like they are in your example.

You'll have to

  • Parse the xml-file with whatever you have to use (jdom)
  • Use an XPath query to retrieve the title nodes. ( //title will do in your case)
  • Get the ID of the grandparent element

These tasks should be easy enough to handle!


In addition to the comment:

The code provided by you:

XPath xpath = XPathFactory.newInstance().newXPath();
NodeList n1 = (NodeList) xpath.evaluate("article/front/article-meta/supplementary-material/caption/title", document, XPathConstants.NODESET);
for (int k = 0; k < n1.getLength();k++)
{
    System.out.println(n1.item(k).getNodeName()+" : "+n1.item(k).getTextContent());
}

works just fine. But the code you provided had some UTF-8 issues in my IDE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM