【问题标题】:JAXB append a xml into a object to be marshaledJAXB 将 xml 附加到要编组的对象中
【发布时间】:2016-10-27 11:40:22
【问题描述】:

我想将一个编组对象(xml 代码)附加到另一个要编组的对象中。

@XmlRootElement
object Child{ 
    ...
}

@XmlRootElement
object Parent{
    @XmlElement
    object Any;
}

编组子:

<child xmlns="namespaceOfChild.org">
    <...>
    <...>
<\child>

我想在 Parent.Any 上设置上面的 xml,以便在编组 Parent 时产生下面的代码。

<parent xmlns="namespaceOfParent.org">
    <any>
        <child xmlns="namespaceOfChild.org">
            <...>
            <...>
        <\child>
    <\any>
<\parent>

请注意,命名空间和 child 的其他属性必须遵循上述代码中的 child tag。当我在 Parent.Any 上设置对象 Child 本身时,我获得了成功,但 Child 的属性看起来像 Parent 的属性。

【问题讨论】:

    标签: java xml jaxb append


    【解决方案1】:

    我明白了!我可以将内部元素Child 保存在Document (org.w3c.dom.Document) 中,并使用该函数将返回document.getDocumentElement()Child 编组为Node (org.w3c. dom.Node)。然后我用这个Node 将对象设置在Parent.Any 中,然后我可以编组Parent

    编组子函数:

    private static Node marshal(Object obj) throws JAXBException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        Document doc = null;
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.newDocument();
        } catch (ParserConfigurationException ex) {
            throw new JAXBException(ex);
        }
    
        context = JAXBContext.newInstance("org.openarchives.oai._2_0.oai_dc");
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(obj, doc);
        return  doc.getDocumentElement();
    }
    

    编组父函数:

    private static void marshal(Object obj, OutputStream stream) throws JAXBException {
        context  = JAXBContext.newInstance("org.openarchives.oai._2");
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(obj, stream);
    }
    

    当我编组 Parent 时,Child 的属性在 &lt;child&gt; 内,而 Parent 的属性在 &lt;parent&gt; 内。

    【讨论】:

      【解决方案2】:

      很好的答案,它引导我在我的环境中找到最终解决方案。

      对于那些试图通过 JAX-RS 和 JERSEY JAXB 实现来实现这一点的人来说,这是我必须做的最后一步。

      假设,我们有一个名为 metadataType 的父类,有一个 XML 类型为“any”的子类,我们想要分配一个名为 Record 的类

      这是我在创建对象时所做的对象的“设置”

      Document doc = null;
          try {
              JAXBContext jaxbContext = JAXBContext.newInstance(OAIPMHtype.class,Record.class,RDF.class);
              Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
              jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.europeana.eu/schemas/ese/ " +
                      "http://www.europeana.eu/schemas/ese/ESE-V3.4.xsd");
              jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              dbf.setNamespaceAware(true);
      
              try {
                  DocumentBuilder db = dbf.newDocumentBuilder();
                  doc = db.newDocument();
      
              } catch (ParserConfigurationException ex) {
                  throw new JAXBException(ex);
              }
              jaxbMarshaller.marshal(record, doc);
          } catch (JAXBException e) {
              throw new JAXBException(e);
          }
              metadataType.setAny(doc.getDocumentElement());
      

      在 Record 类中我必须添加 @XMLRootElement ,例如:

      @XmlRootElement(name = "record",namespace = "http://www.europeana.eu/schemas/ese/")
      

      那么,如果你让 Jersey Marshall 自动或不自动生成这个对象

      <root xmlns="http://rootschema"><metadata><ese:record xmlns:ese="http://www.europeana.eu/schemas/ese/" schemaLocation="http://www.europeana.eu/schemas/ese/ http://www.europeana.eu/schemas/ese/ESE-V3.4.xsd"> <metadata> </root>
      

      【讨论】:

        猜你喜欢
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多