java - Deleting Nodes found with XPath -


i'm trying remove entry (music track) xml document after has been selected , deleted jtable user. xml file looks , has multiple tags same name track identified tag integer in before track data:

<?xml version="1.0" encoding="utf-8"?> <!doctype plist public "-//apple computer//dtd plist 1.0//en" "http://www.apple.com/dtds/propertylist-1.0.dtd"> <plist version="1.0"> <dict>     <key>tracks</key>     <dict>         <key>203</key>         <dict>             <key>track id</key><integer>203</integer>             <key>name</key><string>winter</string>             <key>artist</key><string>daughter</string>             <!-- etc -->             <key>play count</key><integer>2</integer>             <key>skip count</key><integer>1</integer>             <key>track type</key><string>file</string>         </dict>         <key>204</key>         <dict>             <!-- set of track data, etc... -->         </dict>     </dict> </dict> </plist> 

i passing trackid through method using xpath find <key>...</key> , following <dict>...</dict> , want remove both key tag , dict tag directly following key.

public void removetrack (string track, file file) {     try {         docbuilder = docfactory.newdocumentbuilder();     }     catch (parserconfigurationexception pce) {         system.out.println(pce);     }     try {         xpathexpression expr;         document doc = docbuilder.parse(file);         doc.getdocumentelement().normalize();         expr = xpath.compile("/plist//key");         nodelist keys = (nodelist) expr.evaluate(doc, xpathconstants.nodeset);          (int key=0; key<keys.getlength(); key++) {             expr = xpath.compile("/plist/dict/dict/key[contains(text(),'"+track+"']");             node keynode = (node) expr.evaluate(doc, xpathconstants.node);             expr = xpath.compile("/plist/dict/dict/key[contains(text(),'"+track+"')]/following-sibling::*[1]");             node trackdictnode = (node) expr.evaluate(doc, xpathconstants.node);             keynode.getparentnode().removechild(keynode);             trackdictnode.getparentnode().removechild(trackdictnode);             system.out.println(keynode.getnodename()+" : "+keynode.getnodevalue());             system.out.println(trackdictnode.getnodename()+" : "+trackdictnode.getfirstchild());         }     }     catch (saxexception e) {         system.out.println(e);     }     catch (ioexception e) {         system.out.println(e);     }     catch (xpathexpressionexception e) {         system.out.println(e);     } } 

i put system.out calls @ bottom of loop see going on, , got out:

key : null dict : [#text:              ] 

i think problem i'm not connecting nodes files doc.getdocumentelement() or whatever, i'm not sure how apply xpath search this?

your code looks ok , elements wanted should removed. (i hope know have save xml document see changes)

you misinterpret output:

getnodevalue element null , seeing in output tag key. if used gettextcontent() see 203

now, dict tag, fetching first child, text node whitespaces end of <dict> till <key> line bellow , seeing in output.

the 'expected' output (i guessing here wanted), give code:

element trackdictnode = (element) expr.evaluate(doc, xpathconstants.node); .... system.out.println(keynode.getnodename()+" : "+keynode.gettextcontent()); system.out.println(trackdictnode.getnodename()+" : "+trackdictnode.getelementsbytagname("*").item(0).gettextcontent(); 

should print:

key : 203 dict : track id. 

i took dictionary element using nextsibling() of keynode instead of xpath experssion:

element trackdictnode = null; {    node temp = keynode.nextsibling();    while (temp != null && !(temp instanceof element)) temp = temp.nextsibling();    trackdictnode = temp;  } 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -