• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Append Child node in DOM

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I've got an existing XML document, which i'm parsing using the JAXP DOM api, and that works fine. I want to be able to append a new node, and that also works fine, except when i write the dom out to a file, the appended node appears at the end of the existing nodes, instead of the beginning, as it needs to. Here is the XML i'm working on and some snippets of my code to illustrate what i mean:


In the code, the DOM variable is a class-level 'Document' object, which is initialised in the constructor. I guess what I need is something similar to 'nsElement.appendChild(textNode)', maybe 'nsElement.appendChildAtStart(textNode)', but I havent found anything like this in the class documentation on the Sun java site. Is there a method in JAXP/com.wc3.dom to do this, or do i have to jump through some different hoops???
Many thanks for any help!
Robin Harvey.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's exactly what the javadoc for Node#appendChild() says it should do -- append to the end. You should probably use Node#insertBefore() instead.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:
That's exactly what the javadoc for Node#appendChild() says it should do -- append to the end. You should probably use Node#insertBefore() instead.



Not really true (and this is so old of a post I'm sure you're probably not interested, but for the benefit of others that might be searching for the answer:

When one uses the insertBefore method to insert before a root node, an exception is thrown, and you won't get your element placed, sadly.

The Exception looks like this:
 
Marshal
Posts: 25424
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's an old post. But if that's an indirect question you're asking, the answer to it (and to the original post) is that if you want to generate a document which declares a DTD like that, you don't mess about trying to add a bogus node like that. You just output the DOM to the document using a Transformer and set suitable output properties like this:
 
Michael Costello
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
Yes, it's an old post. But if that's an indirect question you're asking, the answer to it (and to the original post) is that if you want to generate a document which declares a DTD like that, you don't mess about trying to add a bogus node like that. You just output the DOM to the document using a Transformer and set suitable output properties like this:
<blockquote>code:


<pre name="code" class="core">xformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
"-//W3C//DTD SVG 20001102//EN");
xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
"http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd";);
</pre>


</blockquote>

I was coming back to this today to report success using exactly this method, slightly differently I suppose:
<blockquote>code:
<pre name="code" class="core">
String dtdString = "\\\\foo\\bar\\baz.DTD";
//...
// set up a document factory document, set up a transformer factory, set up a transformer
//...
transformer.setOutputProperty(javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM, dtdString);
</pre>
</blockquote>

I was pleasantly rewarded with a doctype as intended, and can move on to the other portions of my code, thanks for the validation.
 
It's a tiny ad only because the water is so cold.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
    Bookmark Topic Watch Topic
  • New Topic