什么是JAXBSource构造函数中的contentObject

问题描述

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

JAXBSource(JAXBContext context,Object contentObject);

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

解决方法

简短回答

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

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

JAXBSource source = new JAXBSource(context,person);

但是,我认为这个问题可能会有一些误解。

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

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

如果您要做的只是将一个对象写入XML文件,然后将该XML数据读回一个对象,则可以使用javax.xml.bind.Marshallerjavax.xml.bind.Unarshaller来简化代码。 / p>

更长的答案-带有示例

为澄清起见,下面是一些示例-从最简单的方法开始。

假设我们有以下Person类:

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对象生成的XML的 transformation 规则。这是我们在您的问题中使用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>

但是,如果我想重新排列XML,将在其中放置这些指令。

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

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

    public void transformFileToObject() throws JAXBException,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;