Java & XML-DOM. Change the tag name of an Element object.

D

David Portabella

Using the Java package org.w3c.dom,
I have an Element object.
How can I change the tagName of a given Element?

There is the method Element.getTagName(),
but I did not find the method Element.setTagName() or equivalent.

One solution could be to create a new Element, with the new name, move
all the children and attributes from the old element to the new one,
and to replace the new element for the old one in the tree.

Is there a simpler and more natural solution?
some xml util package (that works with org.w3c.dom)?
 
M

Mike Schilling

David Portabella said:
Using the Java package org.w3c.dom,
I have an Element object.
How can I change the tagName of a given Element?

There is the method Element.getTagName(),
but I did not find the method Element.setTagName() or equivalent.

One solution could be to create a new Element, with the new name, move
all the children and attributes from the old element to the new one,
and to replace the new element for the old one in the tree.

Is there a simpler and more natural solution?
some xml util package (that works with org.w3c.dom)?

Same answer as last time you asked.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

David said:
Using the Java package org.w3c.dom,
I have an Element object.
How can I change the tagName of a given Element?

There is the method Element.getTagName(),
but I did not find the method Element.setTagName() or equivalent.

One solution could be to create a new Element, with the new name, move
all the children and attributes from the old element to the new one,
and to replace the new element for the old one in the tree.

Is there a simpler and more natural solution?
some xml util package (that works with org.w3c.dom)?

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;

public class RenameNode {
private static void print(PrintStream out, Document doc) throws
IOException {
OutputFormat fmt = new OutputFormat();
fmt.setIndenting(true);
XMLSerializer ser = new XMLSerializer(out, fmt);
ser.serialize(doc);
}
public static void main(String[] args) throws Exception {
String xml = "<doc><a>1</a><b>22</b><c>333</c></doc>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new ByteArrayInputStream(xml.getBytes()));
print(System.out, doc);

doc.renameNode(XPathAPI.selectSingleNode(doc.getDocumentElement(),
"/doc/b"), null, "b2");
print(System.out, doc);
}
}

outputs:

<?xml version="1.0" encoding="UTF-8"?>
<doc>
<a>1</a>
<b>22</b>
<c>333</c>
</doc>
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<a>1</a>
<b2>22</b2>
<c>333</c>
</doc>

Maybe you can use that.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top