java - 意外元素(uri :"",本地 :"")。预期元素是(无)

标签 java xml xsd pom.xml cxf-xjc-plugin

我无法使用 cxf-xjc-plugin 将响应 xml 映射到由 xsd 生成的 java。

pom.xml

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-xjc-plugin</artifactId>
                <version>2.3.0</version>
                <configuration>
                    <extensions>
                        <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:2.3.0</extension>
                    </extensions>
                </configuration>
                <executions>
                    <execution>
                        <id>generate-sources-trans</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>xsdtojava</goal>
                        </goals>
                        <configuration>
                            <sourceRoot>${basedir}/src/main/java</sourceRoot>
                            <xsdOptions>
                                <xsdOption>
                                    <xsd>src/main/resources/Response.xsd</xsd>
                                    <packagename>com.test.response</packagename>
                                    <extensionArgs>
                                        <extensionArg>-Xdv</extensionArg>
                                    </extensionArgs>
                                </xsdOption>                                
                                </xsdOption>
                            </xsdOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

响应.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="urn:automatedIDgenerationResponse" xmlns:tns="urn:automatedIDgenerationResponse" xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <!-- definition of simple elements -->
    <complexType name="generatedIDs">
            <sequence>
                <element name="idType" type="string"/>
                <element name="id" type="string"/>
            </sequence>
    </complexType>
    <element name="automatedIDgenerationResponse" type="tns:automatedIDResponse"/>
    <complexType name="automatedIDResponse">
        <sequence>
            <element name="requestID" type="string" nillable="false"/>
            <element name="status" type="string" nillable="false"/>
            <element name="errorCode" type="int" nillable="false"/>
            <element name="errorText" type="string" nillable="true"/>
            <element name="timestamp" type="string" nillable="true"/>
            <element name="generatedIDs" type="tns:generatedIDs"  maxOccurs="unbounded"  />
        </sequence>
    </complexType>
</schema>

AutomatedIDResponse.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "automatedIDResponse", propOrder = {
    "requestID",
    "status",
    "errorCode",
    "errorText",
    "timestamp",
    "generatedIDs"
})
public class AutomatedIDResponse {

    @XmlElement(required = true)
    protected String requestID;
    @XmlElement(required = true)
    protected String status;
    protected int errorCode;
    @XmlElement(required = true, nillable = true)
    protected String errorText;
    @XmlElement(required = true, nillable = true)
    protected String timestamp;
    @XmlElement(required = true)
    protected List<GeneratedIDs> generatedIDs;

    //getter setter
}

GeneratedIDs.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "generatedIDs", propOrder = {
    "idType",
    "id"
})
public class GeneratedIDs {

    @XmlElement(required = true)
    protected String idType;
    @XmlElement(required = true)
    protected String id;
    //getter-setter
}   

主.java

public static void main(String[] args) {
        try {
        String xml=<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
                    <ns3:automatedIDgenerationResponse xmlns:ns2="urn:automatedIDgeneration" xmlns:ns3="urn:automatedIDgenerationResponse">
                        <requestID>432012</requestID>
                        <status>Failure</status>
                        <errorCode>7000</errorCode>
                        <errorText>002098 The subscriber name already exists in the database. [code 2]</errorText>
                        <timestamp>Thu Aug 03 12:37:24 BST 2017</timestamp>
                    </ns3:automatedIDgenerationResponse>;


        StringReader reader = new StringReader(xml);
        JAXBContext jaxbContext2 = JAXBContext.newInstance(AutomatedIDResponse.class);
        Unmarshaller jaxbUnmarshaller2 = jaxbContext2.createUnmarshaller();
        AutomatedIDResponse obj = (AutomatedIDResponse) jaxbUnmarshaller2.unmarshal(reader);
        }catch (JAXBException e) {
            System.out.println("JAXBException" + e);
        }
    }

错误:

JAXBExceptionjavax.xml.bind.UnmarshalException: unexpected element (uri:"urn:automatedIDgenerationResponse", local:"automatedIDgenerationResponse"). Expected elements are (none)

最佳答案

我可以通过这种方式将 xml 转换为对象:

public static AutomatedIDResponse xmlToObject(String xmlString) throws XMLStreamException, UnsupportedEncodingException, JAXBException{
        InputStream stream = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
        JAXBContext jaxbContext2 = JAXBContext.newInstance(AutomatedIDResponse.class);
        Unmarshaller jaxbUnmarshaller2 = jaxbContext2.createUnmarshaller();
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader someSource = factory.createXMLEventReader(stream);
        JAXBElement<AutomatedIDResponse> userElement = jaxbUnmarshaller2.unmarshal(someSource, AutomatedIDResponse.class);
        AutomatedIDResponse responseObject = userElement.getValue();
        return responseObject;
    }

关于java - 意外元素(uri :"",本地 :"")。预期元素是(无),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45483201/

相关文章:

Python 从 xsd 创建 xml

java - xmlunit:ElementSelectors.conditionalBuilder 错误

java - 来自 XML Java Jaxb 的 XSD

java - 如何使用本地 XSD 验证 beans.xml?

java - Gradle 多模块项目编译失败,来自子模块中提到的依赖项的 "classNotFoundExceptions"

java - 生成 PDF 格式的图表

java - JAVA 中 JPG 图像大小调整程序的 0 字节文件

java - 在单个 ListView 中左右对齐

java - 如何将JTextField变成String/Long?

xml - UML 是否有用于它的 XMI 的 XSD?