[jdom-interest] Beta8 DOMBuilder issue

Bradley S. Huffman hip at a.cs.okstate.edu
Tue Apr 2 09:45:00 PST 2002


Peter Coppens writes:

> For some reason, the namespaces got lost. Anybody any suggestions on what I
> am doing wrong.

Yep, DOMBuilder has a bug were a search with getNamespace(String prefix) is
called on a newly created Element (parent == null).  Since parent == null
getNamespace can only check the new Element, but not any higher up on the tree.
Solution is to add the new Element to the document, then add attributes.
Here's a patch.

Brad

*** DOMBuilder.java	Tue Apr  2 11:35:15 2002
--- DOMBuilder.new	Tue Apr  2 11:36:18 2002
***************
*** 411,416 ****
--- 411,424 ----
  
                  Element element = factory.element(localName, ns);
  
+                 if (atRoot) {
+                     // If at root, set as document root
+                     doc.setRootElement(element);
+                 } else {
+                     // else add to parent element
+                     current.addContent(element);
+                 }
+ 
                  // Add namespaces
                  NamedNodeMap attributeList = node.getAttributes();
                  int attsize = attributeList.getLength();
***************
*** 419,425 ****
                      Attr att = (Attr) attributeList.item(i);
  
                      String attname = att.getName();
- 
                      if (attname.startsWith("xmlns")) {
                          String attPrefix = "";
                          colon = attname.indexOf(':');
--- 427,432 ----
***************
*** 478,491 ****
                      }
                  }
  
-                 if (atRoot) {
-                     // If at root, set as document root
-                     doc.setRootElement(element);
-                 } else {
-                     // else add to parent element
-                     current.addContent(element);
-                 }
- 
                  // Recurse on child nodes
                  // The list should never be null nor should it ever contain
                  // null nodes, but some DOM impls are broken
--- 485,490 ----



More information about the jdom-interest mailing list