As I am running this code It is working fine but the out put in xml file is not appropriate.
Out put should be ---
<Company>
<Location>
<Companyname></Companyname> like this
---------------------------
But out put is coming as- javax.xml.transform.stream.StreamResult@9444d1
please help me as I am new in XML.
public void createXmlTree(org.w3c.dom.Document doc) throws Exception {
// this method creates a element node.
Element root = doc.createElement("Company");
// "company is the the first child of xml doc ,means root node."
doc.appendChild(root);
Element child = doc.createElement("Location");
root.appendChild(child);
Element child1 = doc.createElement("CompanyName");
child.appendChild(child1);
// now we will create comment with the help of doc.
Comment comment = doc.createComment("Employee in Associates");
child.appendChild(comment);
Element element = doc.createElement("Employee");
child.appendChild(element);
// now we will create TextNode with the help of doc.and then we will
// place it under element
Text text12 = doc.createTextNode("Anuraag Singh");
element.appendChild(text12);
// now also introducing attributes inside the elements.as key value pair
Element el1 = doc.createElement("Id");
el1.setAttribute("name", "Aish");
root.appendChild(el1);
Text tx23 = doc.createTextNode("Status");
el1.appendChild(tx23);
// now we use transformer object to write the source tree.
// as dom tree we have already created now we will access String from
// there.A character stream that collects its output in a string buffer,
// which can then be used to
// construct a string.
StringWriter stwriter = new StringWriter();
StreamResult result = new StreamResult(stwriter);
// as we have written all the things in doc which is a xml document so
// we are passing it here.
DOMSource source = new DOMSource(doc);
System.out.println("String " + doc);
transformer.transform(source, result);
String xmlString = result.toString();
I have responded to your other thread. Please check it.
Punya Pratap Singh
Ranch Hand
Joined: Nov 23, 2010
Posts: 74
posted
0
I tried that bro.but wasn't working.
g tsuji
Ranch Hand
Joined: Jan 18, 2011
Posts: 357
1
posted
0
[1] The last part of the code seems turning round and round a central purpose... You can do simply this, replacing the whole block starting from StringWriter... onword.
[2] What Santhosh was telling you is to replace the right-hand side of that xmlString line. It makes a lot of sense and should do.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.