开发者

Parse Error Exception

开发者 https://www.devze.com 2023-01-17 08:00 出处:网络
I am parsing a weather data feed and it works with certain locations but errors out with this message on some locations:

I am parsing a weather data feed and it works with certain locations but errors out with this message on some locations:

09-22 10:40:33.364: WARN/System.err(3347): org.apache.harmony.xml.ExpatParser$ParseException: At line 465, column 29: not well-formed 开发者_运维知识库(invalid token)

Any ideas what might be happening?

Here is a snippet of the xml:

                <hour time="11 AM">
                    <url>http://www.....</url>
                    <obsdate>9/22/2010</obsdate>
                    <txtshort>Parcialmente soleado</txtshort>
                    <weathericon>03</weathericon>
                    <temperature>26</temperature>
                    <feelslike>29</feelslike>
                </hour>

                <hour time="12 PM">
                    <url>http://www.....</url>
                    <obsdate>9/22/2010</obsdate>
                    <txtshort>Parcialmente soleado</txtshort>
                    <weathericon>03</weathericon>
                    <temperature>26</temperature>
                    <feelslike>29</feelslike>
                </hour>

Line 465 is the 'hour' tag with the 12pm attribute value. I have logged parse code and it is reading the xml up until it reaches this line.


The error says it's occurring in column 29, and the line that you've said is the line containing the error is only 18 characters long. In all likelihood, this means one of two things: either that line contains non-printing characters that we can't see, one of which is one of the small handful of characters that aren't allowable in XML, or there's an off-by-one error somewhere and the error's occurring in the next line - probably in the URL that you've redacted.


This actually turned out to be an unrecognized character in parsing an XML document. What I did to fix this was to include the encoding type like this: (choose the encoding that matches your XML document)

InputSource inputSource = new InputSource(is);
//inputSource.setEncoding("iso-8859-1");
inputSource.setEncoding("utf-8");


Before attempting to read any xml file, it's always advisable to check for well-formedness of the xml document you are attempting to read. In this case, try to put a well-formedness condition around the xml feed you get from the weather data feed before parsing it. Using C#.Net this can be done as follows:-

XmlDocument doc = new XmlDocument();
doc.loadxml(rawXMLcontent);

If this fails it goes to exception block. You can handle the feed in the exception block accordingly. This assures that you never get any parse exceptions. I hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消