Loop through XML String using XPath - Java

RXC

I have a function I would like to loop through the xml and pull out certain tags.

My xml looks like this:

<Report_Data>
    <Report_Entry>
        <Company>Test</Company>
        <Name>Test Name</Name>
        <Division>Test Division</Division>
    </Report_Entry>
    <Report_Entry>
        <Company>Test 2</Company>
        <Name>Test Name 2</Name>
        <Division>Test Division 2</Division>
    </Report_Entry>
    <Report_Entry>
        <Company>Test 3</Company>
        <Name>Test Name 3</Name>
        <Division>Test Division 3</Division>
    </Report_Entry>
</Report_Data>

Here is my code to loop through:

String comp, name, div, nodeName, NodeValue;
Node node;
try
{
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();

InputSource source = new InputSource(new StringReader(coaFULL));
Document doc2 = (Document) xpath.evaluate("/", source, XPathConstants.NODE);

NodeList nodeList = (NodeList) xpath.compile("/Report_Data/Report_Entry").evaluate(doc2, XPathConstants.NODESET);
System.out.println("NODE LIST LENGTH =" + nodeList.getLength());

String nodeName, nodeValue = "";
Node node;

for(int i = 0; i < nodeList.getLength(); i++)
{
    node = nodeList.item(i);
    node = nodeList.item(i).getFirstChild();
    nodeName = node.getNodeName();
    nodeValue = node.getChildNodes().item( 0 ).getNodeValue();

    if(nodeName.equals("Company"))
    {
        comp = nodeValue;
    }
    else if( nodeName.equals("Name"))
    {
        name = nodeValue;
    }
    else if(nodeName.equals("Division"))
    {
        div = nodeValue;
    }
    System.out.println("COMPANY = " + comp);
    System.out.println("NAME = " + name);
    System.out.println("DIVISION = " + div);
}

When I run my code, only the first value (company) gets an actual value, everything else is blank. I also tried adding node = nodeList.item(i).getNextSibling(); inside of each if statement to grab the next node, but that did not work.

My nodeList does have items in it, over 1000. Is there a problem with this statement: NodeList nodeList = (NodeList) xpath.compile("/Report_Data/Report_Entry").evaluate(doc2, XPathConstants.NODESET);?

Should it be: NodeList nodeList = (NodeList) xpath.compile("/Report_Data/Report_Entry/*").evaluate(doc2, XPathConstants.NODESET);

I tried it with the /* at the end but that caused the nodeList to have every single node in it. I want to make sure that when I grab a Report_Entry node, that I set the string variables to the correct values that correspond to each other.

==========================================================

Solution: It's ugly but my solution was to just go with one loop and use the second list of children nodes with hard coded values:

for(int i = 0; i < nodeList.getLength(); i++)
{
    node = nodeList.item(i);
    tempList = node.getChildNodes();
    System.out.println("TEMP LIST LENGTH =" + tempList.getLength());
    comp = tempList.item(0).getTextContent();
    name = tempList.item(1).getTextContent();
    div = tempList.item(2).getTextContent();
}

Thanks to @hage for his help.

hage

Maybe it's because your node is only the first child?

node = nodeList.item(i);
node = nodeList.item(i).getFirstChild();

I guess nodeList.item(i) will give you the Report_Entrys and their first child is the Company.

You will need to loop over all children of the Company entry

EDIT (regarding your edit):

tempList.item(x) is the Company, Name, and then Division. When you get the first child of this one, you are at the text node (the actual content). And because you try to get the name of this node, you get the #text output (see this).

To get name and value of the nodes, try this (untested)

nodeName = tempList.item(x).getNodeName();
nodeValue = tempList.item(x).getTextContent();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Parse XML Simple String using Java XPath

From Dev

Loop through XML using SQL

From Dev

In SSIS, how to loop through the XML inside a specific element using XPATH in a Foreach NodeList Enumerator

From Dev

Read value from XML String using XPATH Java

From Dev

Not able to loop XML using Xpath using Powershell

From Java

parsing XML using XPath in Java

From Dev

Hadoop - PIG loop on XML nodes using XPath

From Dev

Loop through a list of xml-docs, save xpath tolist

From Java

Loop through all elements in XML using NodeList

From Dev

How to loop through XML using xsl

From Dev

Loop through XML document using XPathNavigator

From Dev

Loop through large XML file using XDocument

From Dev

Loop through XML Nodes using XmlStringStreamer in PHP

From Dev

how to loop through the xml using xsl

From Dev

Loop through simple xml using xslt

From Dev

PowerShell: Parse through xml Nodes via xpath and create output string

From Dev

How to go through each node in a xml file and return true if the name of the node matches a string and false otherwise using XQuery and XPath?

From Dev

Loop through XPath element positions in Python using Selenium

From Dev

How to click through multiple elements with same XPATH in selenium using for loop?

From Dev

loop through results looking for a string using python

From Dev

Loop through string - input as parameter [java]

From Dev

creating string arrays through for loop, in java

From Dev

Getting all attributes of an xml by using java and Xpath

From Dev

Update value of Xml tag using Xpath in Java

From Dev

read xml using xpath query java

From Java

How to read XML using XPath in Java

From Java

How to query XML using namespaces in Java with XPath?

From Java

How to update XML using XPath and Java

From Dev

Java Parsing iTunes XML library using XPath

Related Related

  1. 1

    Parse XML Simple String using Java XPath

  2. 2

    Loop through XML using SQL

  3. 3

    In SSIS, how to loop through the XML inside a specific element using XPATH in a Foreach NodeList Enumerator

  4. 4

    Read value from XML String using XPATH Java

  5. 5

    Not able to loop XML using Xpath using Powershell

  6. 6

    parsing XML using XPath in Java

  7. 7

    Hadoop - PIG loop on XML nodes using XPath

  8. 8

    Loop through a list of xml-docs, save xpath tolist

  9. 9

    Loop through all elements in XML using NodeList

  10. 10

    How to loop through XML using xsl

  11. 11

    Loop through XML document using XPathNavigator

  12. 12

    Loop through large XML file using XDocument

  13. 13

    Loop through XML Nodes using XmlStringStreamer in PHP

  14. 14

    how to loop through the xml using xsl

  15. 15

    Loop through simple xml using xslt

  16. 16

    PowerShell: Parse through xml Nodes via xpath and create output string

  17. 17

    How to go through each node in a xml file and return true if the name of the node matches a string and false otherwise using XQuery and XPath?

  18. 18

    Loop through XPath element positions in Python using Selenium

  19. 19

    How to click through multiple elements with same XPATH in selenium using for loop?

  20. 20

    loop through results looking for a string using python

  21. 21

    Loop through string - input as parameter [java]

  22. 22

    creating string arrays through for loop, in java

  23. 23

    Getting all attributes of an xml by using java and Xpath

  24. 24

    Update value of Xml tag using Xpath in Java

  25. 25

    read xml using xpath query java

  26. 26

    How to read XML using XPath in Java

  27. 27

    How to query XML using namespaces in Java with XPath?

  28. 28

    How to update XML using XPath and Java

  29. 29

    Java Parsing iTunes XML library using XPath

HotTag

Archive