Given this piece of XML
<?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author>
In SAX, it is easy to get attribute values:
@Override public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException{ if(qName.equals("book")){ String bookId = attributes.getValue("id"); ... } }
But to get the value of the text node, for example. <author> tag value, it's pretty complicated ...
private StringBuffer curCharValue = new StringBuffer(1024); @Override public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException { if(qName.equals("author")){ curCharValue.clear(); } } @Override public void characters (char ch[], int start, int length) throws SAXException {
- I'm not sure if the above example even works, what do you think of this approach?
- Is there a better way? (to get the text node)
java xml sax
Eran medan
source share