2

I am parsing this xml

<Root><Status>1</Status><Message>Get call Successful</Message><StatusCode></StatusCode><Item type = 'all' subtype = '0' ><subItem><rank>0</rank><name>humywe12</name><value>4500</value></subItem></Item></Root>

I am parsing it using this code

SAXBuilder builder = new SAXBuilder();
Document doc = null;
xml = xml.replaceAll("\t", "");
StringReader r = new StringReader(xml);
try {
    doc = builder.build(r); <-----here it throws error
} catch (IOException e) {
    // e.printStackTrace();
    throw e;
} catch (Exception e) {
    // e.printStackTrace();
    throw e;
}
return doc;
}

builder.build(r) it throws exception StringIndexOutOfBoundsException.

Am I doing something wrong?

updated ok I have removed only these tags "type = 'all' subtype = '0'" and now it is not giving java.lang.StringIndexOutOfBoundsException. Is there any problem with SAXBUILDER ??

AZ_
  • 21,688
  • 25
  • 143
  • 191
  • 1
    Silly question - "am I doing something wrong"! Of course you are! The machine is always right, and you are always wrong ;-). Post the stack trace, it might have a clue. – Ed Staub Sep 19 '11 at 12:23
  • 1
    Funny, I just try this code, and it works... I got a Document [Document: No DOCTYPE declaration, Root is [Element: ]] My SAXBuilder and Document are comming from org.jdom.Document and org.jdom.input.SAXBuilder; – sly7_7 Sep 19 '11 at 12:33
  • sometimes there are bugs in JDOM parser too :p – AZ_ Sep 19 '11 at 12:33
  • which version are you using of jdom? – AZ_ Sep 19 '11 at 12:34

3 Answers3

2

I believe this was a know JDom bug. See http://www.jdom.org/pipermail/jdom-interest/2000-August/001227.html

You may want to check out one of the latest versions of jdom (as fits within your application).

Saket
  • 45,521
  • 12
  • 59
  • 79
1

Someone can try and identify the error for you, but what I would do is to start with very small xml, say

<Root></Root>

and keep adding to it till I get the error and then see what in the data caused the error.

Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
  • 1
    try double quotes ... `type = "all"`. If the xml document is not created by you it may be extremely difficult to pre-process for this replacement if single quotes can occur elsewhere in the document – Miserable Variable Sep 19 '11 at 12:53
  • Yeah I have also tried it with " " rather than ' ' but still it throws exception. now I have removed those attributes and its working fine now. – AZ_ Sep 19 '11 at 13:06
  • If it is not quotes then maybe it is the space around the `=`. It may have bugs but I doubt it can't handle attributes at all! – Miserable Variable Sep 19 '11 at 13:18
  • it is a known bug http://www.jdom.org/pipermail/jdom-interest/2000-August/001227.html – AZ_ Sep 20 '11 at 04:44
1

Spaces are not allowed between the attribute name and the "=", or between the "=" and the attribute value.

See the spec.

Ed Staub
  • 15,480
  • 3
  • 61
  • 91
  • 1
    I guess he's using a different parser/version. When he removes the attributes it works. – Ed Staub Sep 19 '11 at 15:49
  • I also tried it without spaces but still it throws exception. I am using a an old version of JDOM, might be that was the reason – AZ_ Sep 20 '11 at 04:43