关于Java到XML转换的问题,反之亦然

关于Java到XML转换的问题,反之亦然,java,xml,jakarta-ee,xml-parsing,xml-binding,Java,Xml,Jakarta Ee,Xml Parsing,Xml Binding,我有这样的XML,是从中间件中得到的 <Example library="somewhere"> <book> <AUTHORS_TEST>Author_Name</AUTHORS_TEST> <EXAMPLE_TEST>Author_Name</EXAMPLE_TEST> </book> </Example> 那么有没有办法映射这两个,需要注意的是,XML标

我有这样的XML,是从中间件中得到的

<Example library="somewhere">

   <book>

     <AUTHORS_TEST>Author_Name</AUTHORS_TEST>
     <EXAMPLE_TEST>Author_Name</EXAMPLE_TEST>

   </book>

</Example>

那么有没有办法映射这两个,需要注意的是,XML标记名和类属性名是不同的,所以有人建议用最小的更改来实现这一点吗?Xstream是一个不错的选择,但如果我有大量字段,则很难添加别名,那么除了XStream还有什么更好的选择吗?

有一些很好的库可以为您实现这一点。举个简单的例子

请参见下面的示例:

现在,要将其转换为XML,只需对XStream进行简单调用:

String xml = xstream.toXML(joe);
生成的XML如下所示:

<person>
  <firstname>Joe</firstname>
  <lastname>Walnes</lastname>
  <phone>
    <code>123</code>
    <number>1234-456</number>
  </phone>
  <fax>
    <code>123</code>
    <number>9999-999</number>
  </fax>
</person>

我更喜欢XStream,因为它很容易使用。如果您想做更复杂的事情,比如从XML生成Java类,您应该看看Miquel提到的。但是它更复杂,需要更多的时间来开始。

您正在寻找的是XML绑定,实际上是将XML转换为基于XML模式的java类。这方面的参考实施是可行的,但还有许多其他选择。

注意:我是专家组的负责人和成员

大多数XML绑定库在XML表示中要求每层嵌套一个对象。EclipseLink JAXB MOXy具有@XmlPath扩展,该扩展支持基于XPath的映射来消除此限制

范例

下面演示了如何将@XmlPath扩展应用于您的用例

package forum10511601;

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

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

    @XmlAttribute
    private String library;

    @XmlPath("book/AUTHORS_TEST/text()")
    private String authorsTest;

    @XmlPath("book/EXAMPLE_TEST/text()")
    private String exampleTest;

}
jaxb.properties

要将MOXy指定为JAXB提供程序,您需要在与域模型相同的包中添加一个名为JAXB.properties的文件,并使用以下条目查看

演示

由于MOXy是一个jaxbjsr-222实现,因此您可以使用标准的JAXB运行时api,这些api包含在从javase6开始的JRE/JDK中

package forum10511601;

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

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Example.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum10511601/input.xml");
        Example example = (Example) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(example, System.out);
    }

}
input.xml/Output


XStream是否有助于映射不同的XML标记名和类属性名?是的,您可以为此定义@XStreamaLies。请注意,最初的问题是如何将XML文档转换为类,而不是反过来。另外,顺便说一句,您所拥有的不是架构,而是xml文档。@Miquel:XStream两者都可以!XStream是一个简单的库,可以将对象序列化为XML并再次序列化。感谢@user714965,我已经阅读了它,它非常有用,但是除了XStream之外,我们还能做一些最小的更改吗?使用JAXB,它现在是JDK中的标准。,感谢您的实现有没有办法使用MOXy作为JAXB提供者将XML O/P转换为Java对象?@Prebhathkesav-我不确定我是否理解您的后续问题。我相信我的答案中的XML与XML匹配。一个区别是缺少库属性。它应该与域对象中的属性或其他属性对应吗?是的,它与XML匹配,而库属性对应于property@Prabhathkesav-我已更新答案以包含库属性。如果你还有其他问题,请告诉我。
package forum10511601;

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

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

    @XmlAttribute
    private String library;

    @XmlPath("book/AUTHORS_TEST/text()")
    private String authorsTest;

    @XmlPath("book/EXAMPLE_TEST/text()")
    private String exampleTest;

}
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
package forum10511601;

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

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Example.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum10511601/input.xml");
        Example example = (Example) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(example, System.out);
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<Example library="somewhere">
   <book>
      <AUTHORS_TEST>Author_Name</AUTHORS_TEST>
      <EXAMPLE_TEST>Author_Name</EXAMPLE_TEST>
   </book>
</Example>