java SAX 解析 - 获取文本节点的有效方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2064679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:13:12 来源:igfitidea点击:
SAX parsing - efficient way to get text nodes
提问by Eran Medan
Given this XML snippet
鉴于此 XML 片段
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
In SAX, it is easy to get attribute values:
在 SAX 中,很容易获取属性值:
@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 a text node, e.g. the value of the <author>tag, it is quite hard...
但是要获得一个文本节点的值,例如<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();
}
}
- I'm not sure the above sample is even working, what do you think of this approach?
- Is there a better way? (to get the text node's value)
- 我不确定上面的示例是否有效,您如何看待这种方法?
- 有没有更好的办法?(获取文本节点的值)
回答by ewernli
回答by Venkat
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 ?
}
}

