0

解析しているxmlがあり、非常に長いテキストを含むフィールドがありますが、どういうわけかそれはパーサーによってドロップされます。文字列バッファーまたはビルダーを使用する必要があるため、文字列だけを使用してそれらの文字を取得しているためです。私が欲しいのは、タグだけで値を抽出することです。

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

    if (elementOn) {
     // its not reading element value, though tag is read.
        elementValue = new String(ch, start, length);
        elementOn = false;
    }
}

これはテキストです:

  <description>
   <![CDATA[
  Former CPI MLA Manish Kunjam, who returned on Thursday after, theis is a long long text very long that its being dropped.......so what can be done...................................................................................................
  ]]>
 </description>

ありがとうplzガイド私.....

4

1 に答える 1

2

はい、StringBuilderを使用してください。テキストはチャンクで読み取ることができ、このようなコードの最初の空の行だけを読んでいる可能性があるためです。ドキュメントを参照してください

ヒットするたびにStringBuilderをリセットできますstartElement

private final StringBuilder mStringBuilder = new StringBuilder();
private String elementValue = null;
private boolean elementOn = false;

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

public final void startElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {
    mStringBuilder.setLength(0);
    if (someCondition) elementOn = true;
}

public void endElement(String uri, String localName, String qName) throws SAXException {
     elementValue = mStringBuilder.toString().trim();
     elementOn = false;
}

パーサーは次のことを行います

  <description> -> startElement("description"), reset StringBuilder
   <![CDATA[    -> characters(""), appended to (empty) String 
  Former CPI..  -> characters("Former.."), appended 
  ]]>           -> characters(""), appended
 </description> -> endElement("description"), read text here
 <description> -> startElement("description"), reset StringBuilder all starts again
于 2012-04-26T12:04:42.507 に答える