我已经搜索了与该主题相关的所有问题,但我没有找到答案。我正在尝试将 xml 文档转换为 java 对象。在搜索了执行相同操作的选项后,我发现 jaxb 是执行相同操作的最佳选择。我参考了许多文章并提出了这个不起作用的解决方案。有人可以帮助我吗?
XML 国防部:
<Vectors>
<Vector>
<Id>1</Id>
<Name>abc</Name>
<Desc>abcdefg</Desc>
<Type>body</Type>
<Snippet>
<![CDATA[<total>1234</total>]]>
</Snippet>
</Vector>
<Vector>
<Id>2</Id>
<Name>def</Name>
<Desc>abcdefghigj</Desc>
<Type>body</Type>
<Snippet>
<![CDATA[<total>12345</total>]]>
</Snippet>
</Vector>
</Vectors>
我想创建 2 个类 Vector 的 java 对象。类向量如下所示
package com.abc.TryXmltoJava;
import java.util.List;
import javax.xml.bind.annotation.*;
public class Vector {
int Id,
String Name;
String Desc;
String Type;
@XmlElement
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
@XmlElement
public String getDesc() {
return Desc;
}
public void setDesc(String desc) {
this.Desc = desc;
}
@XmlElement
public String getType() {
return Type;
}
public void setType(String type) {
this.Type = type;
}
}
现在我创建了 Vectors 类,它跟踪两个对象并使用列表实用程序创建一个 Vector 对象列表。
package com.abc.TryXMLtoObject;
import javax.xml.bind.annotation.*;
@XmlRootElement(name="Vectors")
public class Vectors {
@XmlElement(name="Vector")
Vector vector;
public Vector getVector() {
return vector;
}
public void setVector(Vector vector1) {
this.vector = vector1;
}
}
我将尝试使用这两个创建 java 对象列表的类来解组 xml 对象。解组类如下所示..
package com.abc.TryXMLtoObject;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class JAXBExampleUnmarshal {
public static void main(String[] args) {
try {
File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Vectors.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Vectors vectors = (Vectors) jaxbUnmarshaller.unmarshal(file);
System.out.println(vectors);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
运行此代码后..我收到以下错误:
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Vectors"). Expected elements are (none)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at com.abc.TryXMLtoObject.JAXBExampleUnmarshal.main(JAXBExampleUnmarshal.java:17)