java - XML 解码错误

标签 java xml jaxb

我无法弄清楚我收到的以下解码错误。请帮忙。我查看了通过的帖子,但我不知道发生了什么。

意外元素(uri:“http://schemas.xmlsoap.org/soap/envelope/”,本地:“信封”)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tt="http://sample.examples">
<soapenv:Header/>
<soapenv:Body>
  <tt:VersionResponse>
     <tt:SchemaVersionDeclared>1.0.0.1</tt:SchemaVersionDeclared>
  </tt:VersionResponse>
</soapenv:Body>
</soapenv:Envelope>

static public String getVersionFromWSResponseFromJAXB(XMLStreamReader xsr) {
    String versionDataAsXML = badData;

    try {           

        JAXBContext jaxbContext = JAXBContext.newInstance(SchemaVersion.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

        CommonMovementRequirementSchemaVersion schemaVersionDeclared = (SchemaVersion) jaxbUnmarshaller.unmarshal(xsr);

        // Check for empty result
        if (!badData.equalsIgnoreCase(schemaVersionDeclared.getSchemaVersionDeclared())) {
            versionDataAsXML =   schemaVersionDeclared.getSchemaVersionDeclared();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return versionDataAsXML;
}


package org.examples.tools;
import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.stream.StreamSource;

import org.examples.tools.WebServiceTools;



public class TestGetVersionFromWSResponseFromJAXB {

public static void main (String[] args) throws Exception { 

    XMLInputFactory xif = XMLInputFactory.newFactory();
    StreamSource xml = new StreamSource("C:/examples/VersionResponse.xml");
    XMLStreamReader xsr = xif.createXMLStreamReader(xml);

    String result = WebServiceTools.getVersionFromWSResponseFromJAXB(xsr); 
    // Treat result

   System.out.print(result);

}
}

package org.examples.tools;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SchemaVersion", propOrder = {
"schemaVersionDeclared"
})
public class SchemaVersion {

@XmlElement(name = "SchemaVersionDeclared", required = true, defaultValue = "1.0.0.0")
protected String schemaVersionDeclared;

public void setSchemaVersionDeclared(String value) {
    this.schemaVersionDeclared = value;
}

}

最佳答案

您的上下文与您正在解析的 xml 无关:

JAXBContext jaxbContext = JAXBContext.newInstance(SchemaVersion.class);

SchemaVersion {
@XmlElement(name = "SchemaVersionDeclared", required = true, defaultValue = "1.0.0.0")
protected String schemaVersionDeclared;

预期的 xml 是:

<SchemaVersion>
    <SchemaVersionDeclared>xxx</SchemaVersionDeclared>
</SchemaVersion>

关于java - XML 解码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29613314/

相关文章:

xml - 如何检索选定节点在另一个节点集中的位置

java - 删除 xsi :type after marshalling abstract class with hierarchy

java - 在 Spring-JPA 应用程序中使用 JAXB 访问对象

java - 无法使用 JAXBElement 编码(marshal) XmlJavaTypeAdapter?

java - 我的 jComboBox 不对我的 keyListener 使用react,并且 actionPerform 执行奇怪的操作

javascript - 如何使用谷歌地图javascript

android - 动态 RecyclerView 不填充数据

java - Apache Camel 到 SOAP API 的路由

java - 在以下代码中重写是如何工作的?

C# XPath 帮助 - 表达式不工作