你可以在这个网站找到很多这样的问题,但没有一个解决了我的问题。
这是我的XML:
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<lookupInstances xmlns='http://www.pqr.com/awd/rest/v1' name='LKIMGR'>
<lookupParameters>
<lookupParameter name='businessArea'>PQAA</lookupParameter>
<lookupParameter name='MEMBERNUMBER'>ANTHONY1900</lookupParameter>
</lookupParameters>
</lookupInstances>
我已经使用XJC编译器创建了Jaxb类,创建的文件如下所示:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2015.01.13 at 12:15:41 PM IST
//
package com.dsths.ga.awd.main;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="lookupParameters">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="lookupParameter">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"lookupParameters"
})
@XmlRootElement(name = "lookupInstances")
public class LookupInstances {
@XmlElement(required = true)
protected LookupInstances.LookupParameters lookupParameters;
@XmlAttribute
protected String name;
/**
* Gets the value of the lookupParameters property.
*
* @return
* possible object is
* {@link LookupInstances.LookupParameters }
*
*/
public LookupInstances.LookupParameters getLookupParameters() {
return lookupParameters;
}
/**
* Sets the value of the lookupParameters property.
*
* @param value
* allowed object is
* {@link LookupInstances.LookupParameters }
*
*/
public void setLookupParameters(LookupInstances.LookupParameters value) {
this.lookupParameters = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="lookupParameter">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"lookupParameter"
})
public static class LookupParameters {
@XmlElement(required = true)
protected LookupInstances.LookupParameters.LookupParameter lookupParameter;
/**
* Gets the value of the lookupParameter property.
*
* @return
* possible object is
* {@link LookupInstances.LookupParameters.LookupParameter }
*
*/
public LookupInstances.LookupParameters.LookupParameter getLookupParameter() {
return lookupParameter;
}
/**
* Sets the value of the lookupParameter property.
*
* @param value
* allowed object is
* {@link LookupInstances.LookupParameters.LookupParameter }
*
*/
public void setLookupParameter(LookupInstances.LookupParameters.LookupParameter value) {
this.lookupParameter = value;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class LookupParameter {
@XmlAttribute
protected String name;
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
}
}
}
public LookupInstances unmarshallXmlRequest(String xmlReq)
{
LookupInstances instances = null;
try {
JAXBContext jc = JAXBContext.newInstance( LookupInstances.class );
Unmarshaller u = jc.createUnmarshaller();
StringBuffer xmlStr = new StringBuffer( xmlReq );
StringReader strReader = new StringReader( xmlStr.toString() );
StreamSource strSource = new StreamSource(strReader);
Object o = u.unmarshal( strSource );
instances = (LookupInstances)o;
} catch (JAXBException e) {
e.printStackTrace();
}
return instances;
}
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.pqr.com/awd/rest/v1", local:"lookupInstances"). Expected elements are <{}lookupInstances>
以下是一些有用的信息:
XML
下面是XML的一部分。需要注意的一点是xmlns
属性。这是一个特殊属性,它指的是XML文档中的名称空间限定。
<lookupInstances xmlns='http://www.pqr.com/awd/rest/v1' name='LKIMGR'>
<lookupParameters/>
</lookupInstances>
<abc:lookupInstances xmlns:abc='http://www.pqr.com/awd/rest/v1' name='LKIMGR'>
<abc:lookupParameters/>
</abc:lookupInstances>
<lookupInstances name='LKIMGR'>
<lookupParameters/>
</lookupInstances>
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.pqr.com/awd/rest/v1", local:"lookupInstances"). Expected elements are <{}lookupInstances>
此错误表示您没有正确映射命名空间限定。
在JAXB中映射Namesapce限定
JAXB中的名称空间限定是使用包级别的@xmlschema
注释完成的。包级注释位于package-info.java
中。下面是这个类的完整源代码。如果您已经有一个package-info.java
源文件,请确保它正在编译并与其他类一起打包。
@XmlSchema(
namespace = "http://www.pqr.com/awd/rest/v1",
elementFormDefault = XmlNsForm.QUALIFIED)
package your_package;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.pqr.com/awd/rest/v1"
xmlns="http://www.pqr.com/awd/rest/v1"
elementFormDefault="qualified">
</xs:schema>
从xml反编组时遇到异常 } 我为systemUnit类添加了注释@xmlRootelement(name=“systemUnit”)。 有人能帮我解决这个问题吗
嗨,我已经使用wsimport从WSDL生成了java类,但是我已经将响应写到了一个文件*.xml中。但是现在我想读取这个xml文件并填充已经生成的java类。 我试过: XML如下所示: 或者我能做什么?
我知道有很多像这样的问题,但没有一个给我提供正确的答案,所以我来这里。 下面是我得到的XML: 下面是相关的java类:
//我的主类中的解马歇尔代码 例外
我在将XML响应从服务转换为POJO时遇到一个异常。XML如下所示: 我是这样用的: 下面是我的文件的详细信息 包-info.java ItemLink.java ItemLinks.java