鉴于此XML片段
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
在SAX中,很容易获得属性值:
@Override
public void startElement (String uri, String localName,
String qName, Attributes attributes) throws SAXException{
if(qName.equals("book")){
String bookId = attributes.getValue("id");
...
}
}
但要获取文本节点的值,例如<author>
标签的价值,很难......
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
{
//already synchronized
curCharValue.append(char, start, length);
}
@Override
public void endElement (String uri, String localName, String qName)
throws SAXException
{
if(qName.equals("author")){
String author = curCharValue.toString();
}
}
答案 0 :(得分:9)
答案 1 :(得分:1)
public void startElement(String strNamespaceURI, String strLocalName,
String strQName, Attributes al) throws SAXException {
if(strLocalName.equalsIgnoreCase("HIT"))
{
String output1 = al.getValue("NAME");
//this will work but how can we parse if NAME="abc" only ?
}
}