重新排序XML标记 [英] reordering XML tags

查看:105
本文介绍了重新排序XML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一些用于将java对象的内容树写回到XML文件(对象编组)的东西(我知道有很多API用于执行此操作,但是我需要它),我想让用户按照他/她想要的方式对标签进行重新排序,我知道使用JAXB可以解决的注释,但我认为使用注释可能会带来很多痛苦。如果任何人能提供任何好的方法,它会非常有用。

I am trying to implement something which is for writing back the content tree of java object to XML file (object marshalling) (I know there are a lot of APIs for doing that but its required from me), 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. it will be so helpful if any one could offer any good approach.

谢谢

推荐答案

注意:我是 EclipseLink JAXB(MOXy) 领导和 JAXB(JSR-222) 专家组。

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

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

In another answer I described the standard JAXB mechanisms for specifying the order of elements. In this answer I will explain how MOXy's external mapping document can be used to address this part of your question:


我想让用户按照他/她想要的方式重新排序标签,我知道使用注释的
就像JAXB可能解决的那样,但我认为使用
注释可能会带来很大的痛苦。

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

Root 类我使用 @XmlType 注释来指定排序。

In the Root class I have used the @XmlType annotation to specify an ordering.

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的文件,并带有以下条目(请参阅< a href =http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html =nofollow noreferrer>将EclipseLink MOXy指定为您的JAXB提供程序):

To specify MOXy as your JAXB provider you need to add a file called jaxb.properties in the same package as your domain model with the following entry (see Specifying EclipseLink MOXy as Your JAXB Provider):

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

binding-acb.xml

MOXy有一个外部映射文档扩展,允许您覆盖域模型上的映射(请参阅扩展JAXB - 将元数据表示为XML )。我们将使用此文档指定其他订购。

MOXy has an external mapping document extension that allows you to override the mappings on the domain model (see Extending JAXB - Representing Metadata as XML). We will use this document to specify another ordering.

<?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

我们可以使用其他地图文档来提供备用排序。

We can use additional mapping documents to provide alternate orderings.

<?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 实例。

The following demo code demonstrates how to leverage the external mapping document when creating a JAXBContext. We will marshal the same instance of Root three different ways.

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

}

输出

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

Below is the output from running the demo code:

<?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>

这篇关于重新排序XML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆