运行以下代码时出现异常:
JAXBContext context = JAXBContext.newInstance(TestClassesType.class , TestClassType.class);
Unmarshaller um = context.createUnmarshaller();
System.out.println("XMLFILE == " + xmlFilePath);
JAXBElement tcstJaxb = (JAXBElement) um.unmarshal(new FileReader(xmlFilePath));
例外:
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"test-class"). Expected elements are <{}test-classes>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:243)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:238)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105)
我在代码段中编写了TestClassesType.class,TestClassType.class:JAXBContext context = JAXBContext.newInstance(TestClassesType.class , TestClassType.class);
但是当一些xml以test-classes开头时,它能够解组它但是以test-class开头的类,它是无法解组它并抛出上述异常。
使用的类:
TestClassesType
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "test-classesType", propOrder = {
"testClass"
})
public class TestClassesType {
@XmlElement(name = "test-class")
protected List<TestClassType> testClass;
/**
* Gets the value of the testClass property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the testClass property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getTestClass().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link TestClassType }
*
*
*/
public List<TestClassType> getTestClass() {
if (testClass == null) {
testClass = new ArrayList<TestClassType>();
}
return this.testClass;
}
}
TestClassType:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "test-classType", propOrder = {
"content"
})
public class TestClassType {
@XmlElementRef(name = "test-method", type = JAXBElement.class, required = false)
@XmlMixed
protected List<Serializable> content;
@XmlAttribute(name = "name")
protected String name;
/**
* Gets the value of the content property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the content property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getContent().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link String }
* {@link JAXBElement }{@code <}{@link TestMethodType }{@code >}
*
*
*/
public List<Serializable> getContent() {
if (content == null) {
content = new ArrayList<Serializable>();
}
return this.content;
}
/**
* 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;
}
}
我有几个xmls以test-classes开头,有些以test-class标签开头。我想允许这两个。有没有办法实现这个目标?
如此生成的ObjectFactory类如下:
@XmlRegistry
public class ObjectFactory {
private final static QName _TestClasses_QNAME = new QName("", "test-classes");
private final static QName _TestClassTypeTestMethod_QNAME = new QName("", "test-method");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.jaxb.scenario
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link TestClassesType }
*
*/
public TestClassesType createTestClassesType() {
return new TestClassesType();
}
/**
* Create an instance of {@link ParamsType }
*
*/
public ParamsType createParamsType() {
return new ParamsType();
}
/**
* Create an instance of {@link TestCaseType }
*
*/
public TestCaseType createTestCaseType() {
return new TestCaseType();
}
/**
* Create an instance of {@link TestClassType }
*
*/
public TestClassType createTestClassType() {
return new TestClassType();
}
/**
* Create an instance of {@link AssertType }
*
*/
public AssertType createAssertType() {
return new AssertType();
}
/**
* Create an instance of {@link TestMethodType }
*
*/
public TestMethodType createTestMethodType() {
return new TestMethodType();
}
/**
* Create an instance of {@link AssertsType }
*
*/
public AssertsType createAssertsType() {
return new AssertsType();
}
/**
* Create an instance of {@link ParamType }
*
*/
public ParamType createParamType() {
return new ParamType();
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link TestClassesType }{@code >}}
*
*/
@XmlElementDecl(namespace = "", name = "test-classes")
public JAXBElement<TestClassesType> createTestClasses(TestClassesType value) {
return new JAXBElement<TestClassesType>(_TestClasses_QNAME, TestClassesType.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link TestMethodType }{@code >}}
*
*/
@XmlElementDecl(namespace = "", name = "test-method", scope = TestClassType.class)
public JAXBElement<TestMethodType> createTestClassTypeTestMethod(TestMethodType value) {
return new JAXBElement<TestMethodType>(_TestClassTypeTestMethod_QNAME, TestMethodType.class, TestClassType.class, value);
}
}
答案 0 :(得分:1)
test-class
元素是否与@XmlElementDecl
类(或您使用ObjectFactory
注释的类的@XmlRegistry
注释相对应?如果是,则需要包含它当你引导你的JAXBContext
时。
JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
如果您是从XML Schema生成模型,则ObjectFactory
类是唯一需要传递以引导JAXBContext
的类。