'Accessing parameters from XML file in java

I have an inventory record XML file to store the quantity of each item.

<Inventory>
<Item>
    <ManufacturerName>Brand1</ManufacturerName>
    <ProductType>TV</ProductType>
    <Quantity>146</Quantity>
</Item>
<Item>
    <ManufacturerName>Brand2</ManufacturerName>
    <ProductType>TV</ProductType>
    <Quantity>221</Quantity>
</Item>
<Item>
    <ManufacturerName>Brand3</ManufacturerName>
    <ProductType>TV</ProductType>
    <Quantity>36</Quantity>
</Item>
</Inventory>

In my java program, if I get a request for a certain item, I check the quantity of items of that type remaining (Quantity parameter) and, if there are enough, subtract that amount from the XML file. I can do this by looping through each node of the XML and checking the one I want, but I was hoping there was a faster way of accessing one particular node right away. Maybe the structure of the XML file can be changed to make it more accessible but I can't think of one.



Solution 1:[1]

What you are looking for is XPath, here is a small sample:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class GetAllTheChildren {
    public static void main(String[] args) {
    
        try{
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilderFactory.setNamespaceAware(true);
            DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
            Document doc = builder.parse("/home/eugen/Desktop/input.txt");
            
            XPathFactory xPathFactory = XPathFactory.newInstance();
            XPath xpath = xPathFactory.newXPath();
        
            XPathExpression expression = xpath.compile("/Inventory/Item[Quantity>200]/*");
        
            NodeList nodes = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);
        
            for(int i = 0;i<nodes.getLength();i++){
                System.out.println(nodes.item(i).getNodeName());
                System.out.println(nodes.item(i).getTextContent());
            }
        } catch(Exception exception){
            exception.printStackTrace();
        }
    }
}

You probably need to play a bit more with it

Cheers, Eugene.

Solution 2:[2]

XPATH.

How to query XML using XPath (Java).

Solution 3:[3]

There are many ways to deal with XML in Java. You may want to look into Castor, for example. With Castor, you would "load" the XML into a Java class, do your changes in Java, then transform it back to XML.

Solution 4:[4]

DOM4J (an open source xml parser http://dom4j.sourceforge.net/ ) is a good fit.

You can call Element.elements() method to get total count of its child nodes. Use Element.element(String name) to get a specific node with the name. Refer to this API doc: http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/Element.html#element%28java.lang.String%29

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hfontanez
Solution 2 Aravind Yarram
Solution 3 theglauber
Solution 4 user814168