0

私の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());
    } 
}
}
4

1 に答える 1

0

「< p >」をチェックしてください。CDATA に到達したときに true になるブール値を設定できます。このように、CDATA の「< p >」タグと一般的な「< p >」を区別できます。

于 2012-08-10T14:18:28.107 に答える