且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 java 将节点附加到现有的 XML 文件中

更新时间:2022-10-14 23:05:41

如果没有图书馆,你可以做这样的事情:

Element dataTag = doc.getDocumentElement();元素 peopleTag = (Element) dataTag.getElementsByTagName("people").item(0);元素 newPerson = doc.createElement("person");Element firstName = doc.createElement("firstName");firstName.setTextContent("Tom");元素 lastName = doc.createElement("lastName");lastName.setTextContent("汉克斯");newPerson.appendChild(firstName);newPerson.appendChild(lastName);peopleTag.appendChild(newPerson);

结果:

...<人物><firstName>Thomas</firstName><lastName>测试员</lastName><access>false</access><img>tt001.jpg</img></图像></人><人物><firstName>Tom</firstName><lastName>汉克斯</lastName></人></人></数据>

Hi im looking for a solution to append nodes from java into an existing xml file. What i got is an xml file like this

<data>
<people>
    <person>
        <firstName>Frank</firstName>
        <lastName>Erb</lastName>
        <access>true</access>
        <images>
            <img>hm001.jpg</img>
        </images>
    </person>
    <person>
        <firstName>Hans</firstName>
        <lastName>Mustermann</lastName>
        <access>true</access>
        <images>
            <img>hm001.jpg</img>
        </images>
    </person>
    <person>
        <firstName>Thomas</firstName>
        <lastName>Tester</lastName>
        <access>false</access>
        <images>
            <img>tt001.jpg</img>
        </images>
    </person>
</people>
 </data>

what i whant to add is a person node with its elements inside the people element. My big problem is the data node which is root node. If it would be the Person node as root I could solve it. But I can't manage to get the person nodes under the people node.

           <person>
        <firstName>Tom</firstName>
        <lastName>Hanks</lastName>
        <access>false</access>
        <images>
            <img>tt001.jpg</img>
        </images>
    </person>

thanks for your help!

my java code looks as far like this

Element root = document.getDocumentElement();


// Root Element
Element rootElement = document.getDocumentElement();

Collection<Server> svr = new ArrayList<Server>();
svr.add(new Server());

for (Server i : svr) {
    // server elements

    Element server = document.createElement("people");
    rootElement.appendChild(server);
    //rootElement.appendChild(server);

    Element name = document.createElement("person");
    server.appendChild(name);

    Element firstName = document.createElement("firstName");
    firstName.appendChild(document.createTextNode(i.getFirstName()));
    server.appendChild(firstName);
    name.appendChild(firstName);

    Element port = document.createElement("lastName");
    port.appendChild(document.createTextNode(i.getLastName()));
    server.appendChild(port); 
    name.appendChild(port);

    Element access = document.createElement("access");
    access.appendChild(document.createTextNode(i.getAccess()));
    server.appendChild(access); 
    name.appendChild(access);

    String imageName = Main.randomImgNr+"";
    Element images = document.createElement("images");
    //images.appendChild(document.createTextNode(i.getAccess()));
    Element img = document.createElement("img");
    img.appendChild(document.createTextNode(imageName));//i.getImage()));
    images.appendChild(img);            

    server.appendChild(images);
    name.appendChild(images);
    root.appendChild(server);

Without a library you can do something like this:

Element dataTag = doc.getDocumentElement();
Element peopleTag =  (Element) dataTag.getElementsByTagName("people").item(0);

Element newPerson = doc.createElement("person");

Element firstName = doc.createElement("firstName");
firstName.setTextContent("Tom");

Element lastName = doc.createElement("lastName");
lastName.setTextContent("Hanks");

newPerson.appendChild(firstName);
newPerson.appendChild(lastName);

peopleTag.appendChild(newPerson);

Which results:

...
        <person>
            <firstName>Thomas</firstName>
            <lastName>Tester</lastName>
            <access>false</access>
            <images>
                <img>tt001.jpg</img>
            </images>
        </person>
        <person>
            <firstName>Tom</firstName>
            <lastName>Hanks</lastName>
        </person>
    </people>
</data>