0

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.

1

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was looking for something similar to patterns to pull exactly what I need. XPathExpression does that.
2

XPATH.

How to query XML using XPath (Java).

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.