Cannot read <post_body> tag from a XML file
I asked yersterday this question. Now I am having problem with the similar XML file. My problems that it reads all good, until "post_body". It does find it when tetsing in starElement, but cannot print the context of this tag.
Why is that ? I also been adviced to print s in endDocument() but seems that doesnt work. Here is my XML file again and code:<?xml version="1.0" encoding="UTF-8"?>
<root>
<data>
<track clipid="1">
<url>http://www.emp3world.com/to_download.php?id=33254</url>
<http_method>GET or POST</http_method>
<post_body>a=1&b=2&c=3</post_body>
</track>
</data>
</root>
Code:
class MyHandler extends DefaultHandler
{
String str = "";
StringBuil开发者_如何学编程der s = new StringBuilder();
public void startElement(String namespaceURI, String sName, String qName, Attributes atts)
{
s.setLength(0);
if(qName.equals("track"))
{
s.append("ID: ").append(atts.getValue("clipid")).append("\n");
}
if(qName.equals("url"))
{
s.append("URL: ");
}
if(qName.equals("http_method"))
{
s.append("Http method: ");
}
if(qName.equals("header"))
{
s.append("Header: ");
}
if(qName.equals("post_body"))
{
s.append("Post body: ");
}
}
public void endElement(String uri, String localName, String qName)
{
System.out.println(s);
}
public void characters(char[] ch, int start, int length) throws SAXException {
s.append(new String(ch, start, length));
}
}
This is not valid XML:
<post_body>a=1&b=2&c=3</post_body>
The &
symbols are not properly encoded, so this file is malformed and invalid, and XML parsers will not read it.
It should be
<post_body>a=1&b=2&c=3</post_body>
精彩评论