当xml输入作为流并且某些xml元素为空时,SAX解析器无法正常工作

问题描述:

当xml输入作为输入流提供给SAX解析器并且其中一些xml元素为空时,那么解析器的字符方法不是 调用并获得不同的结果。当xml输入作为流并且某些xml元素为空时,SAX解析器无法正常工作

例如,

XML输入:

<root> 
    <salutation>Hello Sir</salutation> 
    <userName /> 
    <parent> 
     <child>a</child> 
    </parent> 
    <parent> 
     <child>b</child> 
    </parent> 
    <parent> 
     <child>c</child> 
    </parent> 
    <success>yes</success> 
    <hoursSpent /> 
</root> 

解析器实现:

public class MyContentHandler implements ContentHandler { 

private String salutation; 
private String userName; 
private String success; 
private String hoursSpent; 
String tmpValue=""; 

public void endElement(String uri, String localName, String qName) throws SAXException { 
if ("salutation".equals(qName)) { 
     userName=tmpValue; 
     } 
}else 
if ("userName".equals(qName)) { 
     userName=tmpValue; 
     } 
}else 
if ("success".equals(qName)) { 
     success=tmpValue; 
     } 
}else 
if ("hoursSpent".equals(qName)) { 
     hoursSpent=tmpValue; 
     } 
} 


public void characters(char[] ch, int begin, int length) throws SAXException { 

    tmpValue = new String(ch, begin, length).trim(); 
} 

主程序:

public class MainProgram{ 

    public static void main(String[] args) throws Exception { 
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 
    SAXParser saxParser = saxParserFactory.newSAXParser(); 

    XMLReader xmlReader = saxParser.getXMLReader(); 

    MyContentHandler contentHandler = new MyContentHandler(xmlReader); 
    xmlReader.setContentHandler(contentHandler); 

    String input = "<root><salutation>Hello Sir</salutation><userName /><parent><child>a</child></parent><parent><child>b</child></parent><success>yes</success><hoursSpent /></root>"; 
    InputStream stream = new ByteArrayInputStream(input.getBytes()); 
    xmlReader.parse(new InputSource(stream)); 
    System.out.println(contentHandler.getUserName()); //prints Hello sir instead of null 
System.out.println(contentHandler.getHoursSpent); //prints yes instead of null 

如果空的xml元素,而不打开指定的ND如下靠近元件,

<userName /> 

代替<userName></userName>,那么不执行该处理程序类的字符()方法和错误的值被设置。只有当我使用input xml作为输入流时才会出现此问题。请帮我解决这个问题

+0

为什么不直接重写[startElement](http://docs.oracle.com/javase/8/docs/api/org/xml/sax/ContentHandler.html#startElement-java.lang.String-java.lang 。String-java.lang.String-org.xml.sax.Attributes-)并让该方法将'tmpValue'重置为空字符串? – VGR

+0

是的,它按预期工作 – pradeep

解析器的行为与指定的完全一样,它是你的代码是错误的。

通常,解析器在开始标记和相应的结束标记之间的characters()方法上进行零对多调用。您需要在startElement()中初始化一个空缓冲区,以字符()附加缓冲区,然后使用endElement()中的累加值。如果解析器将文本分解为多个调用,那么经常发生如果(a)存在,那么您不但会得到错误的结果,而且还会得到错误的结果是文本中的实体引用,或者(b)文本非常长,或者(c)文本恰好跨越在单独的read()调用中从输入流中读取的两个块。