Ask Experts Questions for FREE Help !
Ask
    Leithal130's Avatar
    Leithal130 Posts: 1, Reputation: 1
    New Member
     
    #1

    Jan 17, 2013, 12:02 PM
    Need help creating code to write an XML file
    I have this code:

    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;

    import org.w3c.dom.Attr;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;

    public class WriteXMLFile {

    public static void main(String argv[]) {

    try {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("English");
    doc.appendChild(rootElement);


    Element POS = doc.createElement("POS");
    rootElement.appendChild(POS);

    Element Pronoun = doc.createElement("Pronoun");
    POS.appendChild(Pronoun);

    Element Word = doc.createElement("Word");
    Pronoun.appendChild(Word);

    Element Full = doc.createElement("Full");
    Full.appendChild(doc.createTextNode("Hello"));
    Word.appendChild(Full);
    Full.setAttribute("id", "1");



    Element L = doc.createElement("L");
    L.appendChild(doc.createTextNode("h"));
    Full.appendChild(L);
    L.setAttribute("id", "1");




    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File("H:\\file.xml"));

    transformer.transform(source, result);

    System.out.println("File saved!");


    }
    catch (ParserConfigurationException pce) {
    pce.printStackTrace();
    }
    catch (TransformerException tfe) {
    tfe.printStackTrace();
    }

    }
    }

    I want this code to be able to write an xml file for me and it does that but now I need to be able to use elements over but with different id's. The goal of this test version would be to use element "L" with different id attributes to hold the different letters in the word Hello. The current output is :

    <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    - <English>
    - <POS>
    - <Pronoun>
    - <Word>
    - <Full id="1">
    Hello
    <L id="1">h</L>
    </Full>
    </Word>
    </Pronoun>
    </POS>
    </English>

    The desired ouput would be:

    <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    - <English>
    - <POS>
    - <Pronoun>
    - <Word>
    - <Full id="1">
    Hello
    <L id="1">h</L>
    <L id="2">e</L>
    <L id="3">l</L>
    <L id="4">l</L>
    <L id="5">o</L>
    </Full>
    </Word>
    </Pronoun>
    </POS>
    </English>

Check out some similar questions!

I need to write an XML DTD- HELP! [ 0 Answers ]

This is what I have so far for my code. I attempted a DTD, but I don't know if this is right. Can someone help me write a DTD for my code? I'm new at this, so I'm sorry in advance if I don't get it right off the bat! <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <!DOCTYPE catelogs ...

Creating sitemap as XML & web placement [ 1 Answers ]

Not sure if I'm putting this in the right category. I need some help with figuring out where/how to upload a sitemap to my website. I keep getting an error that the file is not high enough up in my directory. I built the site myself and this is my first foray into anything to do with the...

How PHP Handle XML File? [ 1 Answers ]

Hello, Would you please help? I'm currently using PHP Version 4.3.11. Q#1. If I'd like to created a php page to read a XML file which is generated by Oracle server. What function do I need in php to handle this event? Q#2. Do I need to install a higher php version in order to do so? ...


View more questions Search
 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.