java 使用JAXB解组XML时出现异常

fcg9iug3  于 2023-03-28  发布在  Java
关注(0)|答案(1)|浏览(181)

我在通过JAXB阅读XML文件和基于xsd生成的对象时遇到了问题。我不知道我做错了什么。
我已经看到了同样错误的问题,但不幸的是,没有解决方案的建议对我有帮助。
关于我的系统:Macbook M1 Intellij idea Ultimate Openjdk 19
已经谢谢你的帮助了。
这是你所需要的一切
密码

public Aufgabe4(String path) throws JAXBException {
        File f = new File(path);
        JAXBContext context = JAXBContext.newInstance("generated");
        Unmarshaller unmarshaller = context.createUnmarshaller();
        InvoiceList invoiceList = (InvoiceList) JAXBIntrospector.getValue(
                unmarshaller.unmarshal(f));
}

例外情况

Exception in thread "main" jakarta.xml.bind.UnmarshalException: 
unexpected element (URI:"my.namespace/Invoices", lokal:"invoice-list"). Expected elements are (none)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:693)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.Loader.reportError(Loader.java:230)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.Loader.reportError(Loader.java:225)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:92)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1098)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:527)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:509)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:137)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:518)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3079)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:836)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:605)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:542)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:889)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:825)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1224)
    at java.xml/com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:637)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:218)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:189)
    at jakarta.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:146)
    at jakarta.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:188)
    at jakarta.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:172)
    at Aufgabe4.<init>(Aufgabe4.java:23)
    at Aufgabe4.main(Aufgabe4.java:15)

Process finished with exit code 1

XSD:https://pastebin.com/vPkqH75k XML:https://pastebin.com/hiR3cjsy
把这个修好就好了。

hgtggwj0

hgtggwj01#

溶液

通过JAXB阅读XML文件,生成基于XSD的对象。
使用JAXB通过XJC编译器从XML Schema文件生成Java类。本方案使用Maven项目通过hisrc-higherjaxb-maven-plugin调用XJC
Here是使用标准文件布局的名为Invoices的Maven项目。

Invoices
    src
        main
            java
            resources
                Invoices.xsd
                Invoices.xjb
                simplelogger.properties
        test
            java
                namespace/my/invoices/InvoicesTest.java
            resources
                jvmsystem.arguments
                jvmsystem.properties
            samples
                Invoices01-1.xml
    pom.xml
执行

这是一个独立的Maven项目。您可以使用以下命令运行测试:

mvn clean test
mvn compile exec:java

JAXB类由该插件使用本项目的pom.xml生成

<plugin>
    <groupId>org.patrodyne.jvnet</groupId>
    <artifactId>hisrc-higherjaxb-maven-plugin</artifactId>
    <version>${hisrc-higherjaxb-maven-plugin.version}</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <args>
                    <arg>-no-header</arg>
                    <arg>-XhashCode</arg>
                    <arg>-Xequals</arg>
                    <arg>-XtoString</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>org.patrodyne.jvnet</groupId>
                        <artifactId>hisrc-basicjaxb-plugins</artifactId>
                        <version>${hisrc-basicjaxb.version}</version>
                    </plugin>
                </plugins>
            </configuration>
        </execution>
    </executions>
</plugin>

并且被生成以:

target/generated-sources/xjc/

namespace
└── my
    └── invoices
        ├── AddressType.java
        ├── BankDetailsType.java
        ├── BillingInfo.java
        ├── BillingType.java
        ├── Company.java
        ├── ContactType.java
        ├── CurrencyCodesType.java
        ├── CurrencyType.java
        ├── CustomerInfo.java
        ├── Customer.java
        ├── Invoice.java
        ├── InvoiceList.java
        ├── Items.java
        ├── ItemType.java
        ├── ObjectFactory.java
        ├── Other.java
        ├── package-info.java
        └── TaxesType.java
方法

hisrc-higherjaxb-maven-plugin配置为使用提供的Invoices.xsd schema生成JAXB类。schema提供命名空间"my.namespace/Invoices"JAXB使用命名空间"my.namespace/Invoices"按照自己的命名约定创建Java package名称。
作为一个选项,使用这些XJChisrc-basicjaxb-plugins生成Java内置Object方法的更高级的实现。特别是,示例项目使用toString插件来显示未封送的Invoice对象的 * 人类可读 * 表示。

hisrc-basicjaxb-plugins

<args>
    <arg>-no-header</arg>
    <arg>-XhashCode</arg>
    <arg>-Xequals</arg>
    <arg>-XtoString</arg>
</args>
  • 注意:* 使用XJChisrc-basicjaxb-plugins时,运行时类路径上需要hisrc-basicjaxb-runtime依赖。
测试

JUnit测试类InvoicesTest.java扫描样本文件,并调用方法checkSample(File sample)将每个文件提供给测试人员。对于此项目,将创建一个JAXBContext,并将samples路径中的每个文件 unmarshaledInvoiceList对象。成功后,每个对象将 marshaled 用于日志记录和您的review

演示

namespace.my.invoices.Aufgabe4中有一个Java标准引擎应用程序,它有一个由OP提供的构造函数和一个main(...)方法。

public Aufgabe4(String path) throws JAXBException
{
    File f = new File(path);
    JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    InvoiceList invoiceList = (InvoiceList) JAXBIntrospector.getValue( unmarshaller.unmarshal(f));
    setInvoices(invoiceList);
}

此应用程序的执行使用:

mvn compile exec:java "src/test/samples/Invoices01-1.xml"

相关问题