You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Elliotte Rusty Harold <el...@metalab.unc.edu> on 2001/04/30 03:14:31 UTC

Bug in Document.cloneNode()

I've identified a bug in Document.cloneNode() in Xerces-J 1.3.1. In
essence this fails with a DOMException, Namespace error, whenever the
document I'm clonging uses namespaces. I'm using the JDK 1.3.0 on NT
4.0. Here's some sample code that demnonstrates the problem:

import org.w3c.dom.Document;

import org.apache.xerces.parsers.DOMParser;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;

public class CloneBug {

    public static void main(String[] args) {

        DOMParser parser = new DOMParser();
        for (int i = 0; i < args.length; i++) {
          try {
            parser.parse(args[i]);
            Document input = parser.getDocument();
            Document output = (Document) input.cloneNode(true);
            
            // Document output = resolve(input);
            // need to set encoding on this to Latin-1 and check what
            // happens to UTF-8 curly quotes

           OutputFormat format = new OutputFormat("XML", "ISO-8859-1",
false);
            format.setPreserveSpace(true);
            XMLSerializer serializer
             = new XMLSerializer(System.out, format);
            serializer.serialize(output);
          }
          catch (Exception e) {
            System.err.println(e);
            e.printStackTrace();
          }
        }

    }

}

This program works if the input document does not use namespaces, but
fails like this when it does use namespaces:

D:\books\gold\source\22>java CloneBug test.xml
org.w3c.dom.DOMException: DOM003 Namespace error
org.w3c.dom.DOMException: DOM003 Namespace error
        at org.apache.xerces.dom.AttrNSImpl.<init>(AttrNSImpl.java:136)
        at
org.apache.xerces.dom.DocumentImpl.createAttributeNS(DocumentImpl.jav
a:1325)
        at
org.apache.xerces.dom.DocumentImpl.importNode(DocumentImpl.java:935)
        at
org.apache.xerces.dom.DocumentImpl.importNode(DocumentImpl.java:902)
        at
org.apache.xerces.dom.DocumentImpl.cloneNode(DocumentImpl.java:345)
        at CloneBug.main(CloneBug.java:16)

Here's just one of several test documents on whihc I was able to
reproduce the bug:

<?xml version="1.0" encoding="UTF-8"?>
<PLAYER xmlns:test="http://www.ibiblio.org/test/">
  <GIVEN_NAME>Rey</GIVEN_NAME>
  <SURNAME>Ordonez</SURNAME>
  <POSITION>Shortstop</POSITION>
  <GAMES>153</GAMES>
  <GAMES_STARTED>147</GAMES_STARTED>
  <AT_BATS>505</AT_BATS>
  <RUNS>46</RUNS>
  <HITS>124</HITS>
  <DOUBLES>20</DOUBLES>
  <TRIPLES>2</TRIPLES>
  <HOME_RUNS>1</HOME_RUNS>
  <RBI>42</RBI>
  <STEALS>3</STEALS>
  <CAUGHT_STEALING>6</CAUGHT_STEALING>
  <SACRIFICE_HITS>15</SACRIFICE_HITS>
  <SACRIFICE_FLIES>4</SACRIFICE_FLIES>
  <ERRORS>17</ERRORS>
  <WALKS>23</WALKS>
  <STRUCK_OUT>60</STRUCK_OUT>
  <HIT_BY_PITCH>1</HIT_BY_PITCH>
</PLAYER>


-- 
+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo@metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+ 
|               Java I/O (O'Reilly & Associates, 1999)               |
|            http://metalab.unc.edu/javafaq/books/javaio/            |
|   http://www.amazon.com/exec/obidos/ISBN=1565924851/cafeaulaitA/   |
+----------------------------------+---------------------------------+
|  Read Cafe au Lait for Java News:  http://metalab.unc.edu/javafaq/ | 
|  Read Cafe con Leche for XML News: http://metalab.unc.edu/xml/     |
+----------------------------------+---------------------------------+

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org