JAXB 解组未知 XML 内容的子集

JAXB Unmarshalling an subset of Unknown XML content(JAXB 解组未知 XML 内容的子集)

本文介绍了JAXB 解组未知 XML 内容的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要 unmarshall 未知 XML 内容的子集,使用该未编组的对象,我需要修改一些内容并重新绑定相同的 XML 内容(子集) 与原始 XML.

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.

输入 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>

需要单独解组 <Content> 标记,保持其他 XML 部分相同.需要修改<Content>标签中的元素,并将修改后的XML部分与原文绑定,如下图:

Need to unmarshall the <Content> tag alone, by keeping the other XML part as same. Need to modify the elements in <Content> tag and bind the modified XML part with the original as shown below:

预期输出 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>

我的问题:

  1. 此要求的可能解决方案是什么?(除了 DOM 解析 - 因为 XML 网络非常庞大)

  1. What is the possible solution for this Requirement ? (Except DOM parsing - as XML contnet is very huge)

JAXB2.0 中是否有任何选项可以执行此操作?

Is there any option to do this in JAXB2.0 ?

请就此提出您的建议.

推荐答案

考虑使用 StAX API.

对于给定的示例,此代码使用 Content 元素的根元素创建一个 DOM 文档:

For the given sample, this code creates a DOM document with a root element of the Content element:

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

这可以是 作为 /transform/dom/DOMSource.html" rel="nofollow">DOMSource.

This can then be passed to JAXB as a DOMSource.

在输出时重写 XML 时可以使用类似的技术.

Similar techniques can be used when rewriting the XML on output.

JAXB 似乎不直接接受 StreamSource,至少在 Oracle 1.7 实现中是这样.

JAXB doesn't seem to accept a StreamSource directly, at least in the Oracle 1.7 implementation.

这篇关于JAXB 解组未知 XML 内容的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:JAXB 解组未知 XML 内容的子集

基础教程推荐