简体   繁体   中英

How to add xml nodes few times with VTD-XML?

I have list of items. I parce existing file, if I find those items, then I update the needed fields, but if I don't find those items in file, then I want to add it at the end of file (items xml list). Next in code, so it should find the last item in list loop and write new one after it.

String newNode = createNewItem(listItem); //get xml string 
ap.selectXPath("//item[last()]"); 
index = ap.evalXPath();
if (index != -1) {
    xm.insertAfterElement(newNode);
}

After loop I save changes:

xm.output(updatedFile);

But in results I can see that only one item was added to file. Other missed. How to add all items?

This is a common issue for people new to VTD-XML. Your app logic should take care of the matching process.. and then compose the fragment which you will then insert at the end of your xml file all in one step.

Suppose your list has three items: a, b and c. Your app logic should query the xml document to determine that a and c should be inserted after the end of the xml document. Your app will have to produce the concatnation of xml fragment reprenting a and c... lastly, your app will insert that representation after the last item in your XML document.

Any questions?

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