简体   繁体   中英

Why does java / android require an empty space at the beginning of an XML element to parse it?

I am parsing an XML file for an Android app I am writing and have noticed that without a leading space the first char is replaced with a blank space.

  public void characters(char ch[], int start, int length) { //extends DefaultHandler
if(this.in_mytag || this.in_user_id){
  myXMLParser.setExtractedString(new StringBuffer().append(ch).toString());
}
  }

I am wanting to use the following format in my XML...

<user_id>123</user_id> 

The string that is returned is " 23" (with the 1 lost and an empty space in the string). If I use

<user_id> 123</user_id>

the result is the expected " 123". What am I missing here? Or is this typical and I will need to alter my XML file.

All the SAX characters() method implementations I've seen have observed the offset and length parameters.

Does this version make any difference?

myXMLParser.setExtractedString(new String(ch, start, length));

I'm not sure if this is the reason why, but i noticed you left out the closing identifier in your xml. <user_id>123<user_id> should be <user_id>123</user_id>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM