java-重新排序XML标签

我正在尝试实现将Java对象的内容树写回XML文件(对象编组)的方法(我知道有很多API可以做到这一点,但我需要这样做),我想让用户根据他/她的需要对标签重新排序,我知道像JAXB一样使用注释可以解决这个问题,但是我认为使用注释可能会造成很多麻烦.如果任何人都可以提供任何好的方法,它将非常有帮助.

谢谢

解决方法:

注意:我是EclipseLink JAXB (MOXy)主管,并且是JAXB (JSR-222)专家组的成员.

another answer中,我描述了用于指定元素顺序的标准JAXB机制.在此答案中,我将解释如何使用MOXy的外部映射文档解决您的问题的这一部分:

I want to let the user to reorder the tags as he/she wants, I know
using annotation like what JAXB has may solve that, but I think using
annotation may cause a lot of pain.

在Root类中,我已使用@XmlType批注指定顺序.

package forum11217734;

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlType(propOrder={"c", "b", "a"})
public class Root {

    private String a;
    private String b;
    private String c;

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    public String getC() {
        return c;
    }

    public void setC(String c) {
        this.c = c;
    }

}

jaxb.properties

要将MOXy指定为JAXB提供程序,您需要在与域模型相同的包中添加一个名为jaxb.properties的文件,并带有以下条目(请参见Specifying EclipseLink MOXy as Your JAXB Provider):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

binding-acb.xml

MOXy具有外部映射文档扩展名,使您可以覆盖域模型上的映射(请参见Extending JAXB – Representing Metadata as XML).我们将使用此文档指定其他顺序.

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum11217734">
    <java-types>
        <java-type name="Root">
            <xml-type prop-order="a c b"/>
        </java-type>
    </java-types>
</xml-bindings>

binding-cab.xml

我们可以使用其他映射文档来提供其他顺序.

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum11217734">
    <java-types>
        <java-type name="Root">
            <xml-type prop-order="c a b"/>
        </java-type>
    </java-types>
</xml-bindings>

演示版

以下演示代码演示了在创建JAXBContext时如何利用外部映射文档.我们将以三种不同方式封送相同的Root实例.

package forum11217734;

import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        Root root = new Root();
        root.setA("Foo");
        root.setB("Bar");
        root.setC("Baz");

        // CBA
        JAXBContext cbaContext = JAXBContext.newInstance(Root.class);
        Marshaller cbaMarshaller = cbaContext.createMarshaller();
        cbaMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        cbaMarshaller.marshal(root, System.out);

        // ACB
        Map<String, Object> acbProperties = new HashMap<String, Object>(1);
        acbProperties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum11217734/binding-acb.xml");
        JAXBContext acbContext = JAXBContext.newInstance(new Class[] {Root.class}, acbProperties);
        Marshaller acbMarshaller = acbContext.createMarshaller();
        acbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        acbMarshaller.marshal(root, System.out);

        // CAB
        Map<String, Object> cabProperties = new HashMap<String, Object>(1);
        cabProperties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum11217734/binding-cab.xml");
        JAXBContext cabContext = JAXBContext.newInstance(new Class[] {Root.class}, cabProperties);
        Marshaller cabMarshaller = cabContext.createMarshaller();
        cabMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        cabMarshaller.marshal(root, System.out);
    }

}

输出量

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

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <c>Baz</c>
   <b>Bar</b>
   <a>Foo</a>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <a>Foo</a>
   <c>Baz</c>
   <b>Bar</b>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <c>Baz</c>
   <a>Foo</a>
   <b>Bar</b>
</root>
上一篇:Java-JAXB中的Xml解析问题


下一篇:java-JAXB如何创建具有不同属性值的重复元素