简体   繁体   中英

How to navigate dom tree from XML in java

Below is a sample of the xml I am using, I have striped out some of the fields as they are unnecessary to demonstrate my point.

I am trying to parse the orders from this xml. However, I encounter a problem when I try to parse the product sets for each order. When the first order is processing, instead of adding the 2 sets detailed below, it will add all the sets it can find in the xml into the first order. I am not sure how to get around this as this is all quite new to me. Below is my java...

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();

// Create a list of orders and sub elements
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
nList = doc.getElementsByTagName("order");
setList = doc.getElementsByTagName("set");
orders = new Order[nList.getLength()];
Node nNode = nList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {

    Element eElement = (Element) nNode;
    temp = new Order();
    // Populate order with details from XML
    parseClientDetails(eElement);

    // Add sets
    parseSets();
    temp.setSets(setArray);
    orders[i] = temp;
}

...

    private void parseSets() {
        Node nNode;
        Element element;
        for (int c = 0; c < setList.getLength(); c++) {
            nNode = setList.item(c);
            element = (Element) nNode;

            tempSet = new Set();
            tempSet.setBandwidth(getValue("bandwidth", element));
            tempSet.setCategory(getValue("category", element));
            tempSet.setSet_package(getValue("package", element));
            setArray.add(tempSet);
        }
    }

XML:

<orderSet>
  <order>
    <customer name="SelectCustomerDetails">
        <clientId>UK12345</clientId>
        <etc>...</etc>
    </customer>
    <product>
        <set>
            <category>Silver</category>
            <package>3000IP</package>
            <bandwidth>160</bandwidth>
        </set>
        <set>
            <category>Silver</category>
            <package>3000IP</package>
            <bandwidth>320</bandwidth>
        </set>
    </product>
  </order>
  <order>
  ...
  </order>
</orderSet>

The problem is that you are calling doc.getElementsByTagName("set") which gives you a list of all sets in the entire document. Instead, you need to call it on each order, like this:

 nList = doc.getElementsByTagName("order");
 orders = new Order[nList.getLength()];
 Node nNode = nList.item(i);
 if (nNode.getNodeType() == Node.ELEMENT_NODE) {
     Element eElement = (Element) nNode;

     //get the sets for the current order only
     NodeList setList = eElement.getElementsByTagName("set");

     //now process the sets
 }

You can use the 'javax.xml.xpath' APIs to get the content you need from the XML document. These APIs were introduced in Java SE 5 and provide much more control than 'getElementsByTagName'.

Example

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