Dom2file

// demo.xml
/*<?xml version="1.0" standalone="yes"?>
<!DOCTYPE folks [
<!ELEMENT folks (person)*>
<!ELEMENT person (name | phone | email)*>
<!ELEMENT name (#PCDATA | bold)*>
<!ELEMENT phone (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ELEMENT bold (#PCDATA)>
]>
<folks>
    <person>
        <name>Sam Spade</name>
        <email>[email protected]</email>
    </person>
    <person>
        <name>Sam Diamond</name>
        <email>[email protected]</email>
    </person>
    <person>
        <name>Sam Sonite</name>
        <email>[email protected]</email>
    </person>
</folks>*/
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import com.sun.xml.tree.*;
import org.w3c.dom.*;
import java.io.*;
import java.util.*;
import java.net.*;
import org.apache.xml.serialize.*;
public class BuildXml {
  private Document document;
  private InputStream is;
  private String filename = "demo.xml";
  public BuildXml() {
     getDocument(filename);
     changeContent(document, "Sam Diamond", "[email protected]");   
     toFile(toXML(document));
  public void getDocument(String file) {
      try {
        is = new FileInputStream(file); 
        document = XmlDocumentBuilder.createXmlDocument(is, true);
      catch (Exception e) {
        e.printStackTrace();
  public void changeContent(Document doc, String newname, String newemail) {
    Element root = doc.getDocumentElement();
    NodeList rootlist = root.getChildNodes();
    for (int i=0; i<rootlist.getLength(); i++) {
        Element person = (Element)rootlist.item(i);
        NodeList personlist = person.getChildNodes();
        Element name = (Element)personlist.item(0);
        NodeList namelist = name.getChildNodes();
        Text nametext = (Text)namelist.item(0);
        String oldname = nametext.getData();
        if (oldname.equals(newname)) {
            Element email = (Element)personlist.item(1);
            NodeList emaillist = email.getChildNodes();
            Text emailtext = (Text)emaillist.item(0);
            emailtext.setData(newemail);
  public void toFile(String s) {
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter(filename));
        out.write(s);
        out.close();
    catch (IOException e) {
  public String toXML(Document doc) {
    if (doc == null)
        return "";
    OutputFormat format  = new OutputFormat(doc);
    format.setIndenting(true);
    StringWriter stringOut = new StringWriter();
    XMLSerializer serial = new XMLSerializer(stringOut, format);
    try {
        serial.asDOMSerializer();
        serial.serialize(doc.getDocumentElement());       
    catch (IOException IOe) {
    return stringOut.toString();
public static void main(String args[]) {
    BuildXml buildXml = new BuildXml();
}// error line : serial.serialize(doc.getDocumentElement());

Do you have a question about that code?

Similar Messages

Maybe you are looking for

  • How can I get Verizon FiOS in my condo complex?

    Hello I currently have Verizon DSL 3.0 and I want to get fios Internet only problem is Verizon hasn't installed fiber at my condo complex. Verizon is availabe in my city, can Verizon install fios in my condo? There's about 200 condominiums and half o

  • How do you remove the borders from a table item

    In OAF I have a set of data stored in a table that I must display. However, I must make it look like it is not in a table. I can't use a tablelayout because it is a set of records and records are constantly being added. How do I remove the borders an

  • How do I make a simple full screen menu?

    Hi, I'm new to iDVD and I really need some help here. I hate the menus this comes with and am having a hard time creating what I need. I have a movie I want to use as my menu background. Other than that I just want 2 buttons with text. None of the me

  • [Solved] /var/lib/pacman wrap defined in /etc/fstab

    How come is it that the ext2 wrap of /var/lib/pacman won't work? See here: 1. /var/lib/pacman.db /var/lib/pacman ext2 loop,defaults 0 0 [det@myhost ~]$ sudo mount -a mount: wrong fs type, bad option, bad superblock on /dev/loop1, missing codepage or

  • Pioneer elite plasma connection problem

    I've tried both hdmi outputs on my pioneer tv receiver and i still get no picture. what am i doing wrong?? i've tried switching the outputs with no luck. its a plasma tv with a separate receiver. no hdmi inputs/outputs on plasma unit, only receiver.