简体   繁体   English

Java XPath表达式错误

[英]Java XPath Expression error

I am trying to print a specific node from my XML data file such as Pantone 100 for example. 我试图从我的XML数据文件(例如Pantone 100)中打印特定的节点。 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. 我希望它打印出Pantone 100中的所有属性,例如所有颜色和它们拥有的数据,但是我不确定如何正确格式化XPath编译方式,从而只提取特定的Pantone编号。寻找。

EDIT: The code below outputs null 编辑:下面的代码输出为空

XML Data XML数据

<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 输出空

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: 我不是xpath的专家(今天从字面上学到了它),所以我对此不是100%肯定,但是您有/inventory/product/pantone/text(@=100) ,请尝试以下操作:

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

As I understand it, this will match the Product with the attribute pantone that equals "100" . 据我了解,这将使Product与属性pantone等于"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 . 编辑:签出此页面: Node It is the javadoc for the Node type. 它是Node类型的javadoc。 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 ?). 正如geert3在他/她的回答中所说, getNodeValue()返回节点的值,在这种情况下,它是元素的值,而不是属性(例如:在<element>value</element>中,元素的值element是值),在您的情况下为null因为它为空(如果认为类型为String,则可能是""而不是null ?)。

Try calling Node#getAttributes() and then iterating across the NamedNodeMap with NamedNodeMap#item(int) to get the Node s. 尝试调用Node#getAttributes() ,然后使用NamedNodeMap#item(int)遍历NamedNodeMap以获得Node These should be the attributes (I think, if I am understanding the API correctly). 这些应该是属性(我想,如果我正确理解API的话)。 getNodeName() should be the name of the attribute (eg, pantone ) and getNodeValue() should be the value of the attribute (eg, 100 ). getNodeName()应该是属性的名称(例如pantone ),而getNodeValue()应该是属性的值(例如100 )。

Output is null because getNodeValue is not applicable here. 输出为null,因为getNodeValue在此处不适用。 getTextContent would give you the text between the start and end tags, eg FOOBAR in this example: getTextContent将为您提供开始标记和结束标记之间的文本,例如,在此示例中为FOOBAR:

<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. 或使用a.item(j).getNodeName()a.item(j).getNodeValue()分别检索属性名称或值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM