Saxparsing问题

时间:2011-08-11 09:52:42

标签: android xml-parsing saxparser

我正在使用SaxParsing来解析来自webservice的响应。这里我给的是我得到的xml文件

<HelpDesk xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<entry>
<id>1</id>
<parent>0</parent>
<name>
<![CDATA[ Equipment Tab ]]>
</name>
<description>
<![CDATA[ ]]>
</description>
<content>
<![CDATA[
There are several ways equipment can be added to a booking. Equipment can be added through the tree view of the inventory. It can be added by searching the product code or description. Once equipment is selected for the booking adjustments can be made to&nbsp;the quantity, price, and discount&nbsp;for each line of equipment. Specific pricing models on a per line basis can be created. Discounts can be applied to the entire booking. This will not conflict with any line item discounts. If a discount on the line already exists the overall booking discount will skip the line amount and apply to other applicable lines. Once pricing is complete taxes can be applied as required. Multiple tax rates can be applied by clicking on a tax box. Operator discount authorizations are controlled by the individual operator and not at the group level.
]]>
</content>
<rating>80</rating>
<votes>36</votes>
<views>169</views>
<private>0</private>
<status/>
</entry>
</HelpDesk>

问题是我没有在内容标记中获取完整内容。 任何人都可以提供代码来解析这个xml

由于

2 个答案:

答案 0 :(得分:1)

在xml处理程序的字符方法中使用它

public void characters (char ch[], int start, int length) {
    if (buf!=null) {
        for (int i=start; i<start+length; i++) {
            buf.append(ch[i]);
        }
    }
}

其中buf是字符串构建器。

答案 1 :(得分:1)

您应该在startElement()方法中初始化StringBuffer,并将您在characters()方法获得的字符串追加到String缓冲区

 public void startElement(String namespaceURI, String localName,
          String qName, Attributes atts) throws SAXException {
         if(localName.equals("content")){
            sb= new StringBuffer();

    }
 }

public void characters(char ch[], int start, int length) {
    if(tagName.equals("content")){
        sb.append(new String(ch, start, length));
        //use this string buffer where you want
}