简体   繁体   English

如何将兄弟节点和子节点添加到XML文件中?

[英]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. XML文件和每个节点都应在运行时进行,但是要考虑的是,如果程序停止并再次运行,则应继续通过添加新节点向现有XML文件中添加新单词。在最后一个现有节点之后。 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): 这是我制作XML文件并向其中添加节点的方法,但是它向根节点添加了节点,并且它们没有任何子节点(子节点应在迭代中添加到每个节点中,但是它们被视为一个单独的节点而不是子节点):

    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() 为了使每个XML文件包含唯一项,需要对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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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