JAXB Unmarshalling an subset of Unknown XML content

孤街浪徒 提交于 2019-12-07 15:08:42

问题


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.

Sample Input 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. Need to modify the elements in <Content> tag and bind the modified XML part with the original as shown below:

Expected Output 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)

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

Please provide your suggestions on this.


回答1:


Consider cutting your source document down to size using the StAX API.

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

This can then be passed to JAXB as a DOMSource.

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

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




回答2:


You can annotate an Object property on your class with @XmlAnyElement and by default the unmapped content will be captured as a DOM nodes. If you specify a DomHandler on the @XmlAnyElement then you can control the format. Here is a link to an example where the content is kept as a String.

  • JAXB use String as it is


来源:https://stackoverflow.com/questions/16796418/jaxb-unmarshalling-an-subset-of-unknown-xml-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!