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

Discrepancy in behaviour in adoptNode and its children

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 6
    • 1.3.0
    • xml
    • b56
    • sparc
    • solaris_8

      I am attaching a zip file with the code, xml files and README to reproduce this problem. According to the spec all descendants of the source element are recursively adopted when using the adoptNode of Document.

      However note the FirstName element in following outputs. See output 1 where it fails to get the child nodes. Output 2 is obtained by uncommenting line 58 and 59


      public class Test {
          final static String XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";

          final static String SCHEMA_LANGUAGE =
              "http://java.sun.com/xml/jaxp/properties/schemaLanguage";

          public static void main (String args[]){

              MyErrorHandler eh = new MyErrorHandler();


              try {
                  System.setProperty(DOMImplementationRegistry.PROPERTY,
                      "org.apache.xerces.dom.DOMImplementationSourceImpl");

                  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

                  dbf.setNamespaceAware(true);
                  dbf.setValidating(true);

                  DocumentBuilder docBuilder = dbf.newDocumentBuilder();
                  docBuilder.setErrorHandler(eh);

                  //1. Parsing the first xml file

                  Document document = docBuilder.parse("userInfo.xml");

                  //2. Getting the element

                  Element firstName1 = (Element)document.getElementsByTagName
                                 ("FirstName").item(0);


                 // Uncomment this line and you will see the child nodes values

                  /*System.out.println("TextContent of the node to be adopted: "
                        +firstName1.getTextContent());*/


                  // I am setting the schema validation property once
                  // the xml file with external dtd is parsed

                  dbf.setAttribute(SCHEMA_LANGUAGE, XML_SCHEMA);

                  DocumentBuilder docBuilder1 = dbf.newDocumentBuilder();
                  docBuilder1.setErrorHandler(eh);


                  //3. Parsing the second xml file
                  Document accDocument = docBuilder1.parse("accountInfo.xml");

                  //4. Not sure if right but trying to get the right place to
                  // fit the adopted node

                 Element firstName =(Element) accDocument.getElementsByTagNameNS
                       ("http://www.auctionportal.org/Accounts","FirstName").item(0);

                 Element parent =(Element) firstName.getParentNode();

                  //5. Now invoking the adoptNode
                  Element adoptedAccount = (Element)accDocument.adoptNode(firstName1);

                 //6. Using the adoptedAccount is this right?
                 parent.replaceChild(adoptedAccount,firstName);


                 DOMImplementationRegistry registry = DOMImplementationRegistry
                        .newInstance();
                 DOMImplementationLS impl = (DOMImplementationLS)registry.
                        getDOMImplementation("LS-Load");

                 DOMWriter writer = impl.createDOMWriter();
                 writer.writeNode(System.out,accDocument);

              } catch (Exception e) {
                  e.printStackTrace();
              }

          }
      }



      Output 1
      =============
      <?xml version="1.0"?>
      <acc:Account acc:accountID="1" xmlns="http://www.auctionportal.org/Accounts" xmlns:acc="http://www.auctionportal.org/Accounts" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.auctionportal.org/Accounts accountInfo.xsd">

         <FirstName xmlns="">
         </FirstName><LastName>Green</LastName>
         <UserID>744</UserID>
         <Role><Sell/></Role>




      Output 2
      ==============
      <?xml version="1.0"?>
      <acc:Account acc:accountID="1" xmlns="http://www.auctionportal.org/Accounts" xmlns:acc="http://www.auctionportal.org/Accounts" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.auctionportal.org/Accounts accountInfo.xsd">

         <FirstName xmlns="">King
            </FirstName>
         <LastName>Green</LastName>
         <UserID>744</UserID>
         <Role><Sell/></Role>

            sreddysunw Sunitha Reddy (Inactive)
            bhamehta Bhakti Mehta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: