Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6695393

XML data parsed wrongly in java 6.0

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 6u10
    • xml

      FULL PRODUCT VERSION :
      java version "1.6.0_10-ea"

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP Version 2002

      A DESCRIPTION OF THE PROBLEM :
      XML data with square brackets is being parsed incorrectly.
      The following XML is being parsed incorretly in java 1.6 :

      <?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>

      The java code (below ) generates the following output :

      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 lins are WRONG !!!!!!



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create a file C:\_rf\jrebug.xml with the following content :
      <?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>.

      2. Run the code listed in the "source code" section.


      3. Watch the parsed output. The last two lines are parsed incorrectly.
      Theya are :
      Attribute name is =output6 AND Attribute value is =hh[1]]
      Attribute name is =output7 AND Attribute value is =hh[1]

      THEY SHOULD BE :
      Attribute name is =output6 AND Attribute value is =gg[15]
      Attribute name is =output7 AND Attribute value is =hh[1]




      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      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 =gg[15]
      Attribute name is =output7 AND Attribute value is =hh[1]
      ACTUAL -
      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 PARSED WRONG !!!

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      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){
      //create an instance
      DomParserBug dpe = new DomParserBug();
      //call run example
      dpe.runExample();
      }




      // This error handler uses a Logger to log error messages
      class MyErrorHandler implements ErrorHandler {
      // This method is called in the event of a recoverable error
      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());
      }


      }

      }


      ---------- END SOURCE ----------

            joehw Joe Wang
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: