简体   繁体   中英

How to add sibling and child nodes into a XML file?

The purpose of my program is to add a group of words that are in an array within a loop. Assume in each iteration it should add words from a starting index to an ending index to a node as its children. The XML file and each node should be made in a run-time basis, but there is a consideration that if the program is stopped and then run the program again, it should continue adding new words into the existing XML file by adding a new node after the last existing node. Here is how I get to make the XML file and add nodes to it, but it adds nodes to the root node and they are without any child (the child should be added into each node within an iteration but they are considered as a separate node and not a child node):

    private static void createXML(String word, int id) {

        Element Word= doc.createElement("word"+id);
        rootElement.appendChild(Word);
                //doc.appendChild(Word);

        Attr attr = doc.createAttribute("id");
        attr.setValue(Integer.toString(id));
        Word.setAttributeNode(attr);

        Element Content= doc.createElement("content");
        Content.appendChild(doc.createTextNode(word));
        Word.appendChild(Content);

    }



     private static void saveXML() {
     transformerFactory = TransformerFactory.newInstance();
     transformer = transformerFactory.newTransformer();
     DOMSource source = new DOMSource(doc);
     StreamResult result = new StreamResult(new File("g:\\words.xml"));
     transformer.transform(source, result);   

}

 public static void main(String[] args){

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Element doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("WORDS");
    doc.appendChild(rootElement);

            for(int i=m, i<n; i++)
            createXML(array[m...n], i);

            saveXML();

                }

In case anyone wonder how to do it, here is the change that need to make:

 private static void createXMLnew(String[] words) {

    Element[] word=new Element[words.size()];

    for(int i=0;i<words.size();i++){
        if(i==0)
        {
            word[0] = doc.createElement("word"+0);
    rootElement.appendChild(url[0]);
            Attr attr = doc.createAttribute("id");
    attr.setValue(Integer.toString(0));
    word[0].setAttributeNode(attr);
    Element adr = doc.createElement("adr");
    adr.appendChild(doc.createTextNode(words[0]));
    words[0].appendChild(adr);   
        }
        else{
            word[i] = doc.createElement("word"+i);
            word[0].appendChild(word[i]);
    Attr attr = doc.createAttribute("id");
    attr.setValue(Integer.toString(i));
    word[i].setAttributeNode(attr);
    Element adr = doc.createElement("adr");
    adr.appendChild(doc.createTextNode(words[i]));
    words[i].appendChild(adr); 


        }
    }
}

In this case, the first element will be the father of the rest. To make each XML file to contain unique items, need to make this change to the save()

private static void saveXML(int i) {
 transformerFactory = TransformerFactory.newInstance();
 transformer = transformerFactory.newTransformer();
 DOMSource source = new DOMSource(doc);
 StreamResult result = new StreamResult(new File("g:\\words"+i+".xml"));
 transformer.transform(source, result);   




        docFactory = DocumentBuilderFactory.newInstance();
        docBuilder = docFactory.newDocumentBuilder();

        doc = docBuilder.newDocument();

        rootElement = doc.createElement("WORDS");
            doc.appendChild(rootElement);
//all these added objects need to be defined as static object out of main() function
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM