have the following xml data in a file ( C:\_rf\jrebug.xml ) :
<?xml version="1.0" encoding="UTF-8"?>
<rs>
<data
input3='aa[1]' input4='bb[1]' input6='cc[2]' input8='dd[7]' output2='ee[7]' output4='ff[511]' output6='gg[15]' output7='hh[1]' />
</rs>
I have the following code to parse this XML data :
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;
public class DomParserBug {
Document dom;
public void runExample() {
parseXmlFile("C:\\_rf\\jrebug.xml");
parseDocument();
}
private void parseXmlFile(String filename){
//get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//dbf.setValidating(true);
dbf.setValidating(false);
try {
//Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(new MyErrorHandler());
//parse using builder to get DOM representation of the XML file
dom = db.parse(filename);
}catch(ParserConfigurationException pce) {
pce.printStackTrace();
}
catch(SAXParseException spe) {
spe.printStackTrace();
}
catch(IOException ioe) {
ioe.printStackTrace();
}
catch(SAXException se) {
se.printStackTrace();
}
}
private void parseDocument()
{
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getElementsByTagName("data");
if(nl != null && nl.getLength() > 0)
{
for(int i = 0 ; i < nl.getLength();i++)
{
Element el = (Element)nl.item(i);
NamedNodeMap attrsssss = el.getAttributes();
for (int ii=0; ii<attrsssss.getLength(); ++ii)
{
Node attr = attrsssss.item(ii);
System.out.println("Attribute name is =" +attr.getNodeName() +" AND Attribute value is ="+attr.getNodeValue());
}
}
}
}
public static void main(String[] args){
DomParserBug dpe = new DomParserBug();
dpe.runExample();
}
class MyErrorHandler implements ErrorHandler {
public void error(SAXParseException e) {
System.out.println("error : " +e.toString());
}
// This method is called in the event of a non-recoverable error
public void fatalError(SAXParseException e) {
System.out.println("fatalError : " +e.toString());
}
// This method is called in the event of a warning
public void warning(SAXParseException e) {
System.out.println("warning : " +e.toString());
}
}
}
The parsed output is :
Attribute name is =input3 AND Attribute value is =aa[1]
Attribute name is =input4 AND Attribute value is =bb[1]
Attribute name is =input6 AND Attribute value is =cc[2]
Attribute name is =input8 AND Attribute value is =dd[7]
Attribute name is =output2 AND Attribute value is =ee[7]
Attribute name is =output4 AND Attribute value is =ff[511]
Attribute name is =output6 AND Attribute value is =hh[1]]
Attribute name is =output7 AND Attribute value is =hh[1]
THE LAST TWO LINES ARE SIMPLY WRONG.
With java 5.0 the last two lines are parsed correctly :
Attribute name is =output6 AND Attribute value is =gg[15]
Attribute name is =output7 AND Attribute value is =hh[1]
I have seen this issue only after I upgraded to java 6.0. I have searched the java 6.0 bug database but there is nothing there.
I have also submitted a bug to the bugdatabase last month but have not heared anything.
Anybody have any clue about this ???
Thanks
Edited by: skaushik on Jan 16, 2008 8:24 PM