【问题标题】:Android CDATA parsing with SAX, tag not removed使用 SAX 解析 Android CDATA,未删除标记
【发布时间】:2012-07-09 06:07:08
【问题描述】:

我的 xml 包含一些 cdata

<desc><![CDATA[<p>This is my html text</p>]]></desc> 

我的 SAX 解析器能够解析 xml cdata,但解析后的文本包含标签“CDATA”

<![CDATA[<p>This is my html text</p>]]>

我只想获取 CDATA 中的 html 文本。我可以使用一些字符串函数来删除它,但我想知道这是否是正常的 SAX 行为?

这是我的 SAX 处理程序代码:

public class SAXXMLHandler extends DefaultHandler {
private List<Laptop> laptops;
private Laptop laptop;
private StringBuffer tempSB = new StringBuffer();

public SAXXMLHandler() {
    laptops = new ArrayList<Laptop>();
}

public List<Laptop> getLaptops() {
    return laptops;
}

// Event Handlers
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    tempSB.delete(0, tempSB.length());
    if (qName.equalsIgnoreCase("laptop")) {
        laptop = new Laptop();
        laptop.setModel(attributes.getValue("model"));
    }
}       

public void characters(char[] ch, int start, int length)
        throws SAXException {
    tempSB.append(ch, start, length);
}  

public void endElement(String uri, String localName, String qName)
        throws SAXException {
    if (qName.equalsIgnoreCase("laptop")) {
        laptops.add(laptop);
    } else if (qName.equalsIgnoreCase("id")) {
        laptop.setId(Integer.parseInt(tempSB.toString()));
    } else if (qName.equalsIgnoreCase("desc")) {
        laptop.setDescription(tempSB.toString());
    } 
}
}

【问题讨论】:

  • 不,这不是 SAX 指定的行为方式。它应该在一次或多次调用 characters() 回调中返回 "&lt;![CDATA[""]]&gt;" 之间的内容。
  • 那么可能是什么问题。我添加了我的 SAX 处理程序代码。

标签: android xml sax cdata


【解决方案1】:

请检查“p>”,您可以设置一个布尔值,当到达 CDATA 时它将为真,这样您可以区分“p>” CDATA 中的标记和常见的“p>”。

【讨论】:

    猜你喜欢
    • 2011-04-08
    • 2011-05-25
    • 1970-01-01
    • 2013-11-25
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    相关资源
    最近更新 更多