JAXBSource 的构造函数中的 contentObject 是什么

What is contentObject in the constructor of JAXBSource

我必须创建一个程序,从文件中读取(和写入)一个人。写作部分工作得很好。要阅读我正在使用使用 JAXBSource 的教程的人。在 JAXBSource 的构造函数中:

JAXBSource(JAXBContext context, Object contentObject);

有一个 Object contentObject,我不明白它应该是什么数据类型以及它的用途。也许有人可以帮助我。

简答

contentObject 是您要提供作为输入到您的 XML 过程中的任何对象。例如,此过程可以是 转换 过程。

因此,如果您有一个 Person class,并且您已经创建了一个 person 对象,那么您将拥有:

JAXBSource source = new JAXBSource(context, person);

不过,我认为这个问题可能存在一些误解。

您提到您可以成功地将 person 对象写入文件(我假设为 XML)。但是现在您想将该数据从文件读回新的 person 对象。

但是 JAXBSource 以对象 开始(如上所示),然后通常将该对象写入某个新目标(例如文件)。而且,正如我提到的,它允许沿途转换数据。

如果您只想将一个对象写入 XML 文件,然后将该 XML 数据读回一个对象,那么您可以使用 javax.xml.bind.Marshallerjavax.xml.bind.Unarshaller.

更长的答案 - 带有示例

为了帮助澄清,这里有一些例子 - 从最简单的方法开始。

假设我们有以下 Person class:

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Person {
    
    private String firstName;
    private String lastName;

    // getters and setters not shown here
}

示例 1 - 写入文件

    public void writeObjectToFile() throws JAXBException {
        JAXBContext jc = JAXBContext.newInstance(Person.class);
        Person person = new Person();
        person.setFirstName("Jack");
        person.setLastName("Frost");
        File xmlFile2 = new File("C:/tmp/files/person2.xml");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.marshal(person, xmlFile2);
    }

这会将以下基本内容 XML 写入文件:

<person>
  <firstName>Jack</firstName>
  <lastName>Frost</lastName>
</person>

示例 2 - 从文件读取

这从我们在示例 1 中创建的文件中读取:

    public void readObjectFromFile() throws JAXBException {
        JAXBContext jc = JAXBContext.newInstance(Person.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xmlFile = new File("C:/tmp/files/person2.xml");
        Person person = (Person) unmarshaller.unmarshal(xmlFile);
        System.out.println(person.getFirstName() + " " + person.getLastName());
    }

在上述两个示例中,我们没有对数据执行任何自定义转换 - 我们也不需要使用 JAXBSource(或 JAXBResult - 见下文)。

简单的编组和解组就足够了。

示例 3 - 从对象转换为文件

    public void transformObjectToFile() throws JAXBException,
            TransformerConfigurationException, TransformerException, IOException {
        JAXBContext jc = JAXBContext.newInstance(Person.class);
        Person person = new Person();
        person.setFirstName("John");
        person.setLastName("Smith");

        JAXBSource source = new JAXBSource(jc, person);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer(new StreamSource("C:/tmp/files/person.xslt"));

        Writer writer = new FileWriter("C:/tmp/files/person.xml");
        t.transform(source, new StreamResult(writer));
    }

在示例 3 中有更多代码 - 和一个新的 XSLT 文件,其中包含我们要应用于从 Person 对象。这是我们使用您问题中的 JAXBSource 构造函数的地方。

在我的测试中,我使用了一个 XSLT 文件,它实际上并没有改变 XML - 它只是将它从 Java 对象原样传递到 XML 文件:

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

但如果我想 re-arrange XML,这就是放置这些说明的地方。

示例 4 - 从文件转换为对象

最后一个示例与示例 3 相反。它采用示例 3 文件并创建一个新的 Person 对象:

    public void transformFileToObject() throws JAXBException,
            TransformerConfigurationException, TransformerException {
        JAXBContext jc = JAXBContext.newInstance(Person.class);

        JAXBResult result = new JAXBResult(jc);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer(new StreamSource("C:/tmp/files/person.xslt"));

        t.transform(new StreamSource("C:/tmp/files/person.xml"), result);

        Person person = (Person) result.getResult();
        System.out.println(person.getFirstName() + " " + person.getLastName());
    }

同样,在我的示例中,我使用的转换是“根本没有转换”。

最后,这里是上面的所有相关导入:

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.util.JAXBSource;
import javax.xml.bind.util.JAXBResult;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.Writer;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;