2

我已经搜索了与该主题相关的所有问题,但我没有找到答案。我正在尝试将 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)
4

5 回答 5

3

一开始,you in Vectorsyou must have List<Vector> vectorList,不是单数Vector
此外,stacktrace 中的文本unexpected element (uri:"", local:"Vkectors")告诉我,在 xml 中的某个地方你有Vkectors元素,那是错误的。
最后,我强烈建议您将所有注释放在 get-methods 中,而不是变量,原因有两个:如果我没记错的话,有一些名称冲突,其次,如果您有来自 Hibernate 的代理或类似的东西,字段的注释将消失,但 get 方法的注释将保留。

于 2012-09-20T07:51:22.193 回答
2

您的示例的以下版本对我有用:

矢量图

正如popfalushi 所指出的Vectors应该有一个List属性可以保存Vector.

package com.abc.TryXmltoJava;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="Vectors")
@XmlAccessorType(XmlAccessType.FIELD)
public class Vectors {

    @XmlElement(name="Vector")
    List<Vector> vector;

}

向量

如果您希望在字段上指定 JAXB 注释,则应@XmlAccessorType(XmlAccessType.FIELD)在类型级别指定(请参阅: http ://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html )。此外,默认映射是@XmlElement您不需要明确添加该注释(请参阅: http ://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html )。

package com.abc.TryXmltoJava;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Vector {
    int Id;
    String Name;
    String Desc;
    String Type;
}

JAXBExampleUnmarshal

我更新了您的演示代码以将对象写回 XML,以证明在解组操作期间对象已完全填充。

package com.abc.TryXmltoJava;

import java.io.File;
import javax.xml.bind.*;

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);

            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(vectors, System.out);

        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

}

文件.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>

输出

下面是运行演示代码的输出:

com.abc.TryXmltoJava.Vectors@44fe9319
<?xml version="1.0" encoding="UTF-8"?>
<Vectors>
   <Vector>
      <Id>1</Id>
      <Name>abc</Name>
      <Desc>abcdefg</Desc>
      <Type>body</Type>
   </Vector>
   <Vector>
      <Id>2</Id>
      <Name>def</Name>
      <Desc>abcdefghigj</Desc>
      <Type>body</Type>
   </Vector>
</Vectors>
于 2012-09-20T09:52:51.990 回答
1

您可能希望查看讨论 hos从 xsd 生成类的答案。它还提供了一个资源链接以从原始 xml 生成 xsd。

您的堆栈跟踪还提到了“Vkectors”——它们是源文件中的错字吗?

最后,您可能希望从jaxb 教程开始,然后从那里开始学习。

祝你好运!

于 2012-09-20T07:58:01.587 回答
1

您是否可以先用 Java 创建对象并使用 JAXB 持久化,而不是尝试恢复对象?然后您将看到您的 XML 文件与 JAXB 生成的文件之间的区别。

于 2012-09-20T07:53:15.433 回答
1

首先,您的堆栈跟踪名称Vekctors(注意 k),所以它可能是一个错字。但随后它说“预期的元素是(无)”。在您的Vectors类中,您只声明了一个Vector元素,因此您不能在元素内放置两个元素Vectors

于 2012-09-20T07:56:03.397 回答