Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将xPath节点绑定到JavaBean对象_Java_Xpath_Jaxb - Fatal编程技术网

将xPath节点绑定到JavaBean对象

将xPath节点绑定到JavaBean对象,java,xpath,jaxb,Java,Xpath,Jaxb,我有两种文件可以包含: 物品清单 单一物体 对象列表的示例文件: <?xml version='1.0' encoding='UTF-8'?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:getBeanListResponse xmlns:ns2="namespace">

我有两种文件可以包含:

  • 物品清单
  • 单一物体
  • 对象列表的示例文件:

    <?xml version='1.0' encoding='UTF-8'?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <S:Body>
            <ns2:getBeanListResponse xmlns:ns2="namespace">
                <beanObject>
                    <fieldA>valueA</fieldA>
                    <fieldB>valueB</fieldB>
                    <fieldC>valueC</fieldC>
                </beanObject>
                <beanObject>
                    <fieldA>valueD</fieldA>
                    <fieldB>valueE</fieldB>
                    <fieldC>valueF</fieldC>
                </beanObject>
                <beanObject>
                    <fieldA>valueG</fieldA>
                    <fieldB>valueH</fieldB>
                    <fieldC>valueI</fieldC>
                </beanObject>
            </ns2:getBeanListResponse>
        </S:Body>
    </S:Envelope>
    
    public Object getNodeListValues(String expression) {
        NodeList resultNodes;
        
        try {
            XPathExpression xPathExpression = xpath.compile(expression);
            resultNodes = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
            
            JAXBContext context = JAXBContext.newInstance(BeanObject.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            
            for (int i=0; i<resultNodes.getLength(); i++) {
                BeanObject obj = (BeanObject) unmarshaller.unmarshal(resultNodes.item(i));
                System.out.println(obj);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage()); 
        }
        
        return null;
    }
    
    到目前为止没有任何问题
    JaxB
    成功地将xml数据转换为java对象。但是,我在解析单个对象时出错了。。我使用了相同的xPathExpression,但出现了一些问题。以下是用于对象列表的函数:

    <?xml version='1.0' encoding='UTF-8'?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <S:Body>
            <ns2:getBeanListResponse xmlns:ns2="namespace">
                <beanObject>
                    <fieldA>valueA</fieldA>
                    <fieldB>valueB</fieldB>
                    <fieldC>valueC</fieldC>
                </beanObject>
                <beanObject>
                    <fieldA>valueD</fieldA>
                    <fieldB>valueE</fieldB>
                    <fieldC>valueF</fieldC>
                </beanObject>
                <beanObject>
                    <fieldA>valueG</fieldA>
                    <fieldB>valueH</fieldB>
                    <fieldC>valueI</fieldC>
                </beanObject>
            </ns2:getBeanListResponse>
        </S:Body>
    </S:Envelope>
    
    public Object getNodeListValues(String expression) {
        NodeList resultNodes;
        
        try {
            XPathExpression xPathExpression = xpath.compile(expression);
            resultNodes = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
            
            JAXBContext context = JAXBContext.newInstance(BeanObject.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            
            for (int i=0; i<resultNodes.getLength(); i++) {
                BeanObject obj = (BeanObject) unmarshaller.unmarshal(resultNodes.item(i));
                System.out.println(obj);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage()); 
        }
        
        return null;
    }
    
    但这给了我一个例外,说演员阵容是不可能的:

    com.sun.org.apache.xerces.internal.dom.DeferredElementNSImpl cannot be cast to org.dom4j.Node
    
    根据我的猜测,这里的节点结果是
    文本节点
    ,这是
    \n
    部分,但我不清楚为什么项目列表有效,而此函数无效。。如何获取该xml bean的所有值?还假设我不知道xml文件中对象的结构或字段


    谢谢

    您尝试强制转换到
    org.dom4j.Node
    ,而不是
    org.w3c.dom.Node
    。更改
    import
    语句或使用完全限定的类名

    编辑:由于您在本课程中可能不需要DOM4j,您应该:

    import org.w3c.dom.Node;
    
    在序言中,您的方法应为:

    公共字符串getNodeValue(字符串表达式){
    节点resultNode=null;
    试一试{
    XPathExpression=xpath.compile(表达式);
    resultNode=(Node)xPathExpression.evaluate(xmlDocument,XPathConstants.Node);
    //DOM4j和DOM有相似的方法,但名称不同。
    返回resultNode.getTextContent();
    }捕获(例外e){
    System.out.println(e.getMessage());
    }
    返回null;
    }
    
    我实际上试图转换到
    org.dom4j.Node
    ,试图转换到
    org.w3c.dom.Node
    会出现类型不匹配错误:
    类型不匹配:无法从org.w3c.dom.Node转换到org.dom4j.Node
    是的,这就是问题所在:
    getNodeValue
    函数中不应该有
    org.dom4j
    resultNode
    变量和强制转换都应该是到W3C接口
    org.W3C.dom.Node
    。在Java中,许多类都有相同的简单名称,但您通常只使用其中一个。谢谢,这确实有效。我不知道这个差异,我想我得查一下。这是一个很容易漏掉的bug,编辑器负责管理
    import
    语句(Eclipse的第一选择通常是错误的:-))。是一个标准接口,在Java中有一个嵌入JRE(Xerces的副本)的实现。是一个外部库,它可能是Hibernate的一个依赖项。如果有疑问,请选择DOM。这绝对有帮助,谢谢!
    import org.w3c.dom.Node;