Reputation: 182
HI,
Now i'm going through a project regarding xml manipulation. I want to rewrite the data from one xml file to another. I've attatched my coding and error showing in console. As i'm new to this project i don't know whether this concept is write or not.Could anybody help me to know about this problem?
Would appreciate any pointers..
coding:
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class Test3 {
static Document profile = new Document();
static Element html = new Element("html");
public static void main(String[] args) throws JDOMException {
try{
profile.setRootElement(html);
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><myhtml> <body> <h1 align=\"center\">Profile</h1><hr /> <div class=\"centered\"> <table><tr><td><strong>Name: </strong></td> <td>A</td> </tr> <tr> <td><strong>Age: </strong></td> <td>23</td> <td>programmer</td></tr><tr><td><strong>Email: </strong></td><td>[email protected]</td></tr></table></div><hr /></body></myhtml>";
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new StringReader(xml));
listChildrenOrg(doc.getRootElement(), 0);
}
catch(Exception e){
e.printStackTrace();
}
}
private static void listChildrenOrg(Element root, int depth) {
System.out.println(root.getName());
List children = root.getChildren();
System.out.println(children.size());
Iterator iterator = children.iterator();
while (iterator.hasNext()) {
Element child = (Element) iterator.next();
System.out.println(child);
body.addContent(((Element)child.clone()).detach());
listChildrenOrg(child, depth+1);
}
}
}
output file is blured one..
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>First Try</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">h1 { text-align: center; } div.centered {text-align: center;} div.centered table {margin: 0 auto; text-align: left;}
</style>
</head>
<body>
<h1 align="center">Body part</h1>
<hr />
</body>
<body> <h1 align="center">Profile</h1><hr />
<div class="centered">
<table><tr><td><strong>Name: </strong></td>
<td>A</td> </tr>
<tr> <td><strong>Age: </strong></td>
<td>23</td>
<td>programmer</td></tr>
<tr><td><strong>Email: </strong></td><td>[email protected]</td></tr></table></div>
<hr /></body><h1 align="center">Profile</h1><hr />
<div class="centered">
<table><tr><td><strong>Name: </strong></td>
<td>A</td> </tr>
<tr> <td><strong>Age: </strong></td>
<td>23</td>
<td>programmer</td></tr>
<tr><td><strong>Email: </strong></td>
<td>[email protected]</td></tr></table></div>
<table><tr><td><strong>Name: </strong></td>
<td>A</td> </tr>
<tr> <td><strong>Age: </strong></td>
<td>23</td>
<td>programmer</td></tr>
<tr><td><strong>Email: </strong></td>
<td>[email protected]</td></tr></table>
<tr><td><strong>Name: </strong></td>
<td>A</td> </tr>
<td><strong>Name: </strong></td>
<strong>Name: </strong>
<td>A</td>
<tr> <td><strong>Age: </strong></td>
<td>23</td>
<td>programmer</td></tr>
<td><strong>Age: </strong></td>
<strong>Age: </strong>
<td>23</td><td>programmer</td>
<tr><td><strong>Email: </strong></td><td>[email protected]</td></tr>
<td><strong>Email: </strong></td><strong>Email: </strong>
<td>[email protected]</td><hr />
</html>
Upvotes: 0
Views: 1239
Reputation: 60205
you have to clone and detach the child from its parent element before adding it to the new parent:
html.addContent(((Element)child.clone()).detach());
Upvotes: 0
Reputation: 18068
The issue is here:
Element child = (Element) iterator.next();
System.out.println(child);
html.addContent(child);
child
is reference to the body
element of the original xml document. Its parent is myhtml
You cannot add this as child of another new element in this way.
You need to create a new element for the new xml document. I suggest you to study on "Reference" in Java.
Upvotes: 2