0

我有一个 xml 文件,内容如下

<Event Id="258" Key="123456">
<SubEvent Id="1">
Microsoft will begin selling its &quot;Kinect&quot; full-body motion-sensing game system from November 4, while Sony launched its &quot;Move&quot; motion-controlled gaming system on September 15.  
</SubEvent>
</Event>

解析器处理程序的一部分

public void startElement(String uri, String localName,String qName, 
            Attributes atts) throws SAXException {

    if (qName.equalsIgnoreCase("event")) {
         scored = false;
         event = true;
                   if (//my condition){
                        scored = true; 
                   }
                   if (scored){
                           sb= new StringBuffer();
     }
}

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

    if (event) {
        event = false;
    }

    if (subevent) {
                    if (scored){
                            sb.append(new String(ch, start, length));
                    }
        subevent = false;

    }
}

但这只会返回“微软将开始销售其”

我读到解析器可能会执行多个字符调用。如何将不同的调用连接在一起?你能解释一下逻辑流程吗?

附加信息:我打印了所有元素,看来句子的其余部分没有被调用,为什么?

 Start Element :Event
 Start Element :SubEvent
 SubEvent: 
 Microsoft will begin selling its 
 End Element :SubEvent
 End Element :Event
 Start Element :Event
 Start Element :SubEvent
 SubEvent: 
 Nintendo will sell a new version of its DS handheld device that can play games and show movies in 3D without glasses sometime before March 2011. 
 End Element :SubEvent
 End Element :Event
4

2 回答 2

0

Solved.

public void endElement(String uri, String localName,
    String qName) throws SAXException {
         if (qName.equalsIgnoreCase("event")) {
                scored = false;
            }
            if (qName.equalsIgnoreCase("subevent")){
                subevent=false;
            }
}
于 2013-08-13T01:53:53.017 回答
0

characters(char ch[], int start, int length)方法不会读取完整的行,您应该将字符存储在 a中并在方法StringBuffer中使用它。endElement

public void endElement(String uri, String localName, String qName) throws SAXException
{

     if (qName.equalsIgnoreCase("event")) 
     {
        System.out.println(sb.toString());
     }
     sb = new StringBuffer();
}
于 2013-08-12T10:49:12.440 回答