Java XML parsing: taking inner XML using SAX

2019-02-27 04:43发布

问题:

I'm parsing an XML file with SAX and at some point I need the inner XML of an element. E.g., for the following XML

<a name="abc">
  <b>def</b>
<a>

I need to get the inner XML for the element a, which would be

<b>def<b>

What's the easiest way to do that?

Thanks.

Ivan

回答1:

For this type of situation I suggest using 2 content handlers. The first is responsible for finding the relevant part of the document, and the second for processing the content. My answer to a similar question (see link below) demonstrates how to implement this approach:

  • Using SAX to parse common XML elements