xml 排序java_java-重新排序XML标签

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

谢谢

解决方法:

在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).我们将使用此文档指定其他顺序.

xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"

package-name="forum11217734">

binding-cab.xml

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

xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"

package-name="forum11217734">

演示版

以下演示代码演示了在创建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 acbProperties = new HashMap(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 cabProperties = new HashMap(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);

}

}

输出量

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

Baz

Bar

Foo

Foo

Baz

Bar

Baz

Foo

Bar

标签:reflection,jaxb,xml,java

来源: https://codeday.me/bug/20191201/2078569.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值