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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Result is not appropriate

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.

TransformerFactory transFact = TransformerFactory.newInstance();
Transformer transformer = transFact.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

// 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();

File file = new File("C:/WebApplications/newxml.xml");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file)));
bw.write(xmlString);
bw.flush();
bw.close();

}


 
Ranch Hand
Posts: 81
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Don't create 2 seperate threads for the same issue.
 
Punya Pratap Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks for your update,I will remember it.please also post some reply for the issue. Thanks.
 
Santhosh ayiappan
Ranch Hand
Posts: 81
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I have responded to your other thread. Please check it.
 
Punya Pratap Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I tried that bro.but wasn't working.
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
[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.
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic