Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add new element (Tags) with DOM

Status
Not open for further replies.

aas1611

Programmer
Dec 14, 2001
184
DE
Hi all,

I have a task like the following:

My original XML-file is like this:
Code:
<Karteikasten>  
	<Karteikarte>  
		<Stichwort>XML</Stichwort>    
		<Sachgebiet>Webtechnologie</Sachgebiet>   
		. . .
	</Karteikarte>  
        <Karteikarte>  
                . . .
        </Karteikarte>  
</Karteikasten>

And I have to add a new Tag between <Stichwort> and <Sachgebiet> called <Fachgebiet>, and a comment at the end, so it's going to be like this:
Code:
<Karteikasten>  
	<Karteikarte>  
		<Stichwort>XML</Stichwort>    
         [b]<Fachgebiet>EDV</Fachgebiet>[/b] 
		<Sachgebiet>Webtechnologie</Sachgebiet>   
		. . .
	</Karteikarte>  
        <Karteikarte>  
                . . .
        </Karteikarte>  
[b]<!-- Comment here -->[/b]
</Karteikasten>

I already started the DOM program, something like this:
Code:
// Modifizieren der XML-Struktur
// Ändern und Einfügen eines Elementes

import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;
import java.io.*;
import org.apache.xerces.dom.*; // neu
// importe für DateFormater:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Aufgabe3 {
	DOMParser myParser;
	File xmlFile;
	Document xmlDoc;
	Element docRoot;
	
	public Aufgabe3() {}
	
	public static void main(String args[]) {
		try {
		
			DOMParser myParser = new DOMParser();
			
			// Parsen des Dokumentes
			File myFile = new File("Karteikasten.xml");
			myParser.parse("file:///" + myFile.getAbsolutePath());
			
			Document myDoc = myParser.getDocument();
			
			Element kk = myDoc.getDocumentElement();
			Element fachgebiet = myDoc.createElement("Fachgebiet");
			kk.appendChild(fachgebiet);
			fachgebiet.appendChild(myDoc.createTextNode("EDV"));
			
			Node element = myDoc.getElementsByTagName("Sachgebiet").item(0);
			element = element.getNextSibling();
			Node parent = element.getParentNode();
			parent.insertBefore(element);
			
			myDoc.createComment("Comment here");
			
			
		} catch (java.io.IOException ioe) {
			System.out.println(ioe.getMessage());
		} catch (org.xml.sax.SAXException saxex) {
			System.out.println(saxex.getMessage());
		}
	}
}

But it didn't work. I'm stuck. I think I completely messed it up. Can somebody help me?

Thanks a lot!

Andre
 
Try:
Code:
Document myDoc = myParser.getDocument();

Element Fachgebiet = myDoc.createElement("Fachgebiet");
Fachgebiet.appendChild(myDoc.createTextNode("EDV"));

Node Sachgebiet = myDoc.getElementsByTagName("Sachgebiet").item(0);
Node parent = Sachgebiet.getParentNode();
parent.insertBefore(Fachgebiet, Sachgebiet);

Jon

"I don't regret this, but I both rue and lament it.
 
Hello Jon,

I've tried your code. Well, it didn't give me error messages or anything on compiling and running, but also it didn't give me the result. The Karteikasten.xml remains unchanged (These files are in the same directory, of course).

What is with the comment at the end of the XML file?


Andre
 
You need to save the DOM object to the file. I have never done this, as personally I use .net. But try something like:
Code:
try{
	String outputURL = "output.xml";

	StreamResult result = new StreamResult(new FileOutputStream(outputURL));

	TransformerFactory transFactory = TransformerFactory.newInstance();
	Transformer transformer = transFactory.newTransformer();

	transformer.transform(myDoc, result);	

} catch (Exception e) {
	
}

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top