简体   繁体   English

JAXB解组未知XML内容的子集

[英]JAXB Unmarshalling an subset of Unknown XML content

I have a requirement to unmarshall a subset of Unknown XML content, with that unmarshalled object, I need modify some contents and re-bind the same XML content(subset) with the Original XML. 我需要unmarshall组未知XML内容的子集,使用该未编组的对象,我需要修改一些内容并使用原始XML重新绑定相同的XML内容(子集)。

Sample Input XML: 示例输入XML:

<Message>
    <x>
    </x>
    <y>
    </y>
    <z>
    </z>
    <!-- Need to unmarshall this content to "Content" - java Object -->
    <Content>
        <Name>Robin</Name>
        <Role>SM</Role>
        <Status>Active</Status>
    </Content>
.....
</Message>

Need to unmarshall the <Content> tag alone, by keeping the other XML part as same. 需要通过将其他XML部分保持相同来单独解组<Content>标记。 Need to modify the elements in <Content> tag and bind the modified XML part with the original as shown below: 需要修改<Content>标记中的元素并将修改后的XML部分与原始文件绑定,如下所示:

Expected Output XML: 预期输出XML:

<Message>
    <x>
    </x>
    <y>
    </y>
    <z>
    </z>
    <!-- Need to unmarshall this content to "Content" - java Object -->
    <Content>
        <Name>Robin_123</Name>
        <Role>Senior Member</Role>
        <Status>1</Status>
    </Content>
.....
</Message>

My Questions: 我的问题:

  1. What is the possible solution for this Requirement ? 这个要求的可能解决方案是什么? (Except DOM parsing - as XML contnet is very huge) (除了DOM解析 - 因为XML contnet非常庞大)

  2. Is there any option to do this in JAXB2.0 ? JAXB2.0有没有选择呢?

Please provide your suggestions on this. 请提供您的建议。

Consider cutting your source document down to size using the StAX API . 考虑使用StAX API将源文档缩小到适当大小。

For the given sample, this code creates a DOM document with a root element of the Content element: 对于给定的示例,此代码创建一个DOM文档,其中包含Content元素的根元素:

class ContentFinder implements StreamFilter {
  private boolean capture = false;

  @Override public boolean accept(XMLStreamReader xml) {
    if (xml.isStartElement() && "Content".equals(xml.getLocalName())) {
      capture = true;
    } else if (xml.isEndElement() && "Content".equals(xml.getLocalName())) {
      capture = false;
      return true;
    }
    return capture;
  }
}

XMLInputFactory inFactory = XMLInputFactory.newFactory();
XMLStreamReader reader = inFactory.createXMLStreamReader(inputStream);
reader = inFactory.createFilteredReader(reader, new ContentFinder());
Source src = new StAXSource(reader);
DOMResult res = new DOMResult();
TransformerFactory.newInstance().newTransformer().transform(src, res);
Document doc = (Document) res.getNode();

This can then be passed to JAXB as a DOMSource . 然后可以将其作为DOMSource 传递给JAXB

Similar techniques can be used when rewriting the XML on output. 在输出上重写XML时可以使用类似的技术。

JAXB doesn't seem to accept a StreamSource directly, at least in the Oracle 1.7 implementation. JAXB似乎不直接接受StreamSource ,至少在Oracle 1.7实现中是这样。

You can annotate an Object property on your class with @XmlAnyElement and by default the unmapped content will be captured as a DOM nodes. 您可以使用@XmlAnyElement在类上注释Object属性,默认情况下,未映射的内容将作为DOM节点捕获。 If you specify a DomHandler on the @XmlAnyElement then you can control the format. 如果指定DomHandler@XmlAnyElement那么你可以控制的格式。 Here is a link to an example where the content is kept as a String . 以下是一个示例的链接,其中内容保存为String

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM