how to revise xml parser exception “<?xml … ?> prolog must be at the document start”

StackOverflow https://stackoverflow.com/questions/3545003

  •  30-09-2019
  •  | 
  •  

문제

i use follows code to get a XML Document (by JDOM in java):

SAXBuilder builder = new SAXBuilder();
Document doc= builder.build(new URL("http://www.sasatuan.com/api/api.php"));

and i get a Exception:

org.jdom.input.JDOMParseException: Error in building:
 http://www.sasatuan.com/api/api.php:1: <?xml ... ?> occurs after content.  The
<?xml ... ?> prolog must be at the document start.: http://www.sasatuan.com/api/
api.php:1: <?xml ... ?> occurs after content.  The <?xml ... ?> prolog must be a
t the document start.
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java:473)
        ...

i type this URL http://www.sasatuan.com/api/api.php in IE,it's be ok,but in firefox or chrome,it's same error.

this xml header not at the document start,so jdom can't read it.

i think trim() the xml content before build can revise this problem, i want know how to do this.

thanks for help :)

도움이 되었습니까?

해결책

Do you have other XML files which are similarly broken that you need to read? If not, I would personally try to get in touch with the owner of the website. That XML is simply invalid - and it would probably be an easy job to fix it, which would help everyone rather than just you.

If that doesn't appeal, it would probably be easiest to download the content as a string, trim it in memory, and then load it. You could mess around trying to skip the relevant bits of the input stream, but that's more likely to be fiddly.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top