使用EclipseLink MOXy在JAXB编组中重复XML标记

时间:2012-08-21 10:27:49

标签: jaxb eclipselink moxy xml-binding

有人可以帮助我使用EclipseLink MOXy使用JAXB编组生成带有重复标记的XML。

@XmlPath("ExecRpt/Pty/@ID") --"ABC"
@XmlPath("ExecRpt/Pty/@ID") --"ABD"
@XmlPath("ExecRpt/Instrmt/@Exch") --"AAA"

我期待结果:

 <ExecRpt> <pty ID="ABC"/> <Instrmt Exch="AAA"/><pty ID="ABD"/>  </ExecRpt>

使用以下方法我将从带注释的bean生成到XML。

 JAXBContext.createMarshaller()
 Marshaller.marshal()

提前多多感谢

1 个答案:

答案 0 :(得分:1)

以下是如何使用EclipseLink JAXB (MOXy)@XmlPath扩展名映射用例的示例。

<强> ExecRpt

您可以指定要映射到@XmlPath("Pty[2]/@ID")的元素的位置。

package forum12052961;

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

@XmlRootElement(name="ExecRpt")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"field1", "field2", "field3"})
public class ExecRpt {

    @XmlPath("Pty[1]/@ID")
    String field1;

    @XmlPath("Instrmt/@Exch")
    String field2;

    @XmlPath("Pty[2]/@ID")
    String field3;

}

<强> jaxb.properties

要将MOXy指定为JAXB提供程序,您需要在与域模型相同的程序包中包含名为jaxb.properties的文件,并带有以下条目(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)。

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

<强>演示

package forum12052961;

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum12052961/input.xml");
        ExecRpt execRpt = (ExecRpt) unmarshaller.unmarshal(xml);

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

}

<强> input.xml中/输出

<?xml version="1.0" encoding="UTF-8"?>
<ExecRpt>
   <Pty ID="ABC"/>
   <Instrmt Exch="AAA"/>
   <Pty ID="ABD"/>
</ExecRpt>

了解更多信息