简体   繁体   English

Java在Netbeans中解组复杂类型

[英]java unmarshalling complex types in Netbeans

I am trying to unMarshall XML data from a file with the following Schema file, using NetBeans 8.2, in a java Web Application. 我试图在Java Web应用程序中使用NetBeans 8.2从具有以下架构文件的文件中解编XML数据。

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://xml.netbeans.org/schema/xSchema"
            xmlns:tns="http://xml.netbeans.org/schema/xSchema"
            elementFormDefault="qualified">

        <xsd::complexType name="xItem">

                <xsd:sequence>
                    <xsd:element name="item1" type="xsd:string"/>
                    <xsd:element name="item2" type="xsd:string"/>
                    <xsd:element name="item3" type="xsd:int"/>

                    <xsd:element name="x-price">
                        <xsd:complexType>
                            <xsd:sequence>
                                <xsd:element name = "item4" type="xsd:string"/>
                                <xsd:element name = item5" type = "xsd:float"/>
                                <xsd:element name = "item6" type="xsd:string"/>
                            </xsd:sequence>
                        </xsd:complexType>
                    </xsd:element>

                </xsd:sequence>

            </xsd:complexType>

            <xsd:element name = "xList">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="x_details" type="tns:xItem" minOccurs="0" maxOccurs="unbounded" />
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>


</xsd:schema>

I created an XML Document using NetBeans and populated that with data, matching the Schema file. 我使用NetBeans创建了XML文档,并使用与Schema文件匹配的数据填充了XML文档。 The XML schema is bound to the Java Web Application using Jaxb. XML模式使用Jaxb绑定到Java Web Application。 I have created the unmarshalling code using the jaxbu method, which looks like this 我已经使用jaxbu方法创建了解组代码,如下所示

   xList currentx = new xList();

    //UnMarshal data from XML to Object
    try {
        javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(currentx.getClass().getPackage().getName());
        javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        currentx = (xList) unmarshaller.unmarshal(fileHandle); //NOI18N
    } catch (javax.xml.bind.JAXBException ex) {
        // XXXTODO Handle exception
        java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
    }

The resulting ArrayList currentx, contains all the instances of item1, item2 and item 3. It does not however appear to contain items 4 and 5. 'fileHandle' is a FILE object, and is obviously pointing to the correct file. 结果ArrayList currentx包含item1,item2和item 3的所有实例。但是,它似乎不包含item 4和5。“ fileHandle”是FILE对象,显然指向正确的文件。 The generated files include classes for xItem and a nested static class for x-price, but whenever I try to retrieve data from the nested class, I get a null pointer exception for items4, 5 and 6, but can read items 1,2 and 3. The unmarshalling function appears to perform without any exceptions being created. 生成的文件包括xItem的类和x-price的嵌套的静态类,但是每当我尝试从嵌套的类中检索数据时,我都会得到item4、5和6的空指针异常,但可以读取项目1,2和3.取消编组功能似乎可以正常运行,没有创建任何异常。 Am I using the right methods for unmarshalling all the data or am I missing something to deal with the x-price ( items 4, 5 and 6)? 我是否使用正确的方法来解组所有数据,或者我缺少处理x价格的东西(第4、5和6项)? How is that done ? 怎么做的?

XLM first data record and header: XLM第一个数据记录和标头:

<ns1:xList
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns:ns1='http://xml.netbeans.org/schema/xSchema'
    xsi:schemaLocation='http://xml.netbeans.org/schema/xSchema xSchema1.xsd'>
    <ns1:x_details>
        <ns1:item1>AA</ns1:item1>
        <ns1:item2>AA</ns1:item2>
        <ns1:aitem3>212</ns1:item4>
        <ns1:x-price>
            <ns1:item4>GBP</ns1:item4>
            <ns1:item5>75.26</ns1:item5>
            <ns1:item6>04/12/2018</ns1:item6>
        </ns1:x-price>
    </ns1:x_details>

In your example Schema your schema seems to be invalid: 在示例架构中,您的架构似乎无效:

<xsd:element name = item5" type = "xsd:float"/>

and should be 并且应该是

<xsd:element name = "item5" type = "xsd:float"/>

also it is hard to help if your real data is missing (your XML). 如果您的真实数据丢失(您的XML),也很难提供帮助。 Could you add a full XML example? 您可以添加完整的XML示例吗?

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

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