1

I am trying to print a specific node from my XML data file such as Pantone 100 for example. I would like it to print out all the attributes from pantone 100 such as all the colors and the data they hold but I am unsure of how to format the XPath compile correctly in a way where it will pull only the specific pantone number I'm looking for.

EDIT: The code below outputs null

XML Data

<inventory>
    <Product pantone="100" blue="7.4" red="35" green="24"> </Product>
    <Product pantone="101" blue="5.4" red="3" rubine="35" purple="24"> </Product>
    <Product pantone="102" orange="5.4" purple="35" white="24"> </Product>
    <Product pantone="103" orange="5.4" purple="35" white="24"> </Product>
    <Product pantone="104" orange="5.4" purple="35" white="24"> </Product>
    <Product pantone="105" orange="5.4" purple="35" white="24"> </Product>
    <Product pantone="106" black="5.4" rubine="35" white="24" purple="35" orange="5.4"> </Product>
</inventory>

Code

import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import java.io.IOException;
import org.xml.sax.SAXException;

public class XPathDemo {

    public static void main(String[] args)
            throws ParserConfigurationException, SAXException,
            IOException, XPathExpressionException {

        DocumentBuilderFactory domFactory
                = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse("data.xml");
        XPath xpath = XPathFactory.newInstance().newXPath();
        // XPath Query for showing all nodes value
        XPathExpression expr = xpath.compile("/inventory/Product[@pantone='100']");

        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue());
        }
    }
}

Output null

2 Answers 2

3

I'm no expert with xpath (literally learned about it today) so I am not 100% certain about this, but you have /inventory/product/pantone/text(@=100), instead try this:

/inventory/Product[@pantone='100']

As I understand it, this will match the Product with the attribute pantone that equals "100".

As for printing the data, I am not sure, but hopefully this will get you on the right track.

Edit: Check out this page: Node. It is the javadoc for the Node type. As geert3 said in his/her answer getNodeValue() returns the value of the node, which in this case is the value of the element, not the attributes (for example: in <element>value</element> the value of the element element is value) which in your case is null because it's empty (if it thought the type was String maybe it would be "" instead of null?).

Try calling Node#getAttributes() and then iterating across the NamedNodeMap with NamedNodeMap#item(int) to get the Nodes. These should be the attributes (I think, if I am understanding the API correctly). getNodeName() should be the name of the attribute (e.g., pantone) and getNodeValue() should be the value of the attribute (e.g., 100).

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

2 Comments

this seems to work but I get "null" as an output. not sure whats wrong
@ssj3goku878 I have searched Google for the Node javadoc and done my best to update the answer to be what you are looking for. Try that and report back.
2

Output is null because getNodeValue is not applicable here. getTextContent would give you the text between the start and end tags, e.g. FOOBAR in this example:

<Product pantone="100" blue="7.4" red="35" green="24">FOOBAR</Product>`.

However if you want to print all attribute values for your resultset:

    NodeList nodes = (NodeList)result;
    for (int i = 0; i < nodes.getLength(); i++)
    {
        NamedNodeMap a = nodes.item(i).getAttributes();
        for (int j=0; j<a.getLength(); j++)
            System.out.println(a.item(j));
    }

or use a.item(j).getNodeName() or a.item(j).getNodeValue() to retrieve attribute name or value respectively.

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.