gpt4 book ai didi

java - XML 到 Java 对象

转载 作者:数据小太阳 更新时间:2023-10-29 02:50:07 26 4
gpt4 key购买 nike

我搜索了与该主题相关的所有问题,但没有找到答案。我正在尝试将 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 对象。类 Vector 如下所示

    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)

最佳答案

一开始,你在Vectors你必须有 List<Vector> vectorList , 不是单数 Vector .
此外,来自堆栈跟踪的文本 unexpected element (uri:"", local:"Vkectors")告诉我,在 xml 的某处你有 Vkectors元素,那是错误的。
最后,我强烈建议您将所有注释都放在 get-methods 上,而不是变量上,原因有两个:如果我没记错的话,会发生一些名称冲突,其次,如果您有来自 Hibernate 或类似东西的代理,来自字段的注释将消失,但 get-methods 的注释将保留。

关于java - XML 到 Java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12508098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com