Results 1 to 2 of 2
Thread: Modification in XML file
- 04-10-2010, 03:23 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
Modification in XML file
please help me i cant move due to this. i want to delete the nodes in xml file.here is what i have tried.
Java Code:import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class Delete { public void del(List<String> li) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; Document doc = null; for(int u=0;u<li.size();u++){ // list contains d nodevalue whose parent hav to b deleted //eg: li has value "Close_0" try { db = dbf.newDocumentBuilder(); doc = db.parse("tstfile.xml"); String nodename = new String(); String nodevalue = new String(); NodeList listofmenus = doc.getElementsByTagName("Testcase"); for (int i = 0; i < listofmenus.getLength(); i++) { Node testcase = listofmenus.item(i); NodeList nodelist1 = testcase.getChildNodes(); for (int j = 0; j < nodelist1.getLength(); j++) { Node node1 = nodelist1.item(j); if (node1.getNodeName().equals("Menu")) { NodeList nodelist2 = node1.getChildNodes(); for (int k = 0; k < nodelist2.getLength(); k++) { Node node2 = nodelist2.item(k); if (node2.getNodeName().equals("Nonterminal")) { nodename = (node2.getNodeName()); nodevalue = (node2.getFirstChild() .getNodeValue()); if(nodevalue.equals(li.get(u))){ System.out.println(nodevalue +""+li.get(u)); testcase.removeChild(node1); doc.normalize(); } } } } } } } catch (ParserConfigurationException e1) { e1.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } }
here is d xml file(tstfile.xml)
Moderator Edit: Code tags added -- although code was not formatted to begin withJava Code:<?xml version="1.0" ?> <Testsuite> <Mode>STRUCTURAL</Mode> <Testcase> <Test>256</Test> <Length>3</Length> <Component> <Window>mynotepad_0</Window> <Nonterminal>AutoGenLabel_0</Nonterminal> <Eventtype>LEFTCLICK</Eventtype> <Eventvalue>UNKNOWN</Eventvalue> </Component> <Menu> <Window>mynotepad_0</Window> <Nonterminal>File_0</Nonterminal> </Menu> <Menu> <Window>mynotepad_0</Window> <Nonterminal>Open_0</Nonterminal> </Menu> </Testcase> <Testcase> <Test>257</Test> <Length>3</Length> <Component> <Window>mynotepad_0</Window> <Nonterminal>AutoGenLabel_0</Nonterminal> <Eventtype>LEFTCLICK</Eventtype> <Eventvalue>UNKNOWN</Eventvalue> </Component> <Menu> <Window>mynotepad_0</Window> <Nonterminal>File_0</Nonterminal> </Menu> [B][I]<Menu> <Window>mynotepad_0</Window> // this type of nodes i want to delete on //basis of Close_0 <Nonterminal>[U]Close_0[/U]</Nonterminal> </Menu> [/I][/B] </Testcase> <Testcase> <Test>258</Test> <Length>3</Length> <Component> <Window>mynotepad_0</Window> <Nonterminal>AutoGenLabel_0</Nonterminal> <Eventtype>LEFTCLICK</Eventtype> <Eventvalue>UNKNOWN</Eventvalue> </Component> <Menu> <Window>mynotepad_0</Window> <Nonterminal>File_0</Nonterminal> </Menu> <Menu> <Window>mynotepad_0</Window> <Nonterminal>Edit_0</Nonterminal> </Menu> </Testcase> <Testcase> <Test>259</Test> <Length>3</Length> <Component> <Window>mynotepad_0</Window> <Nonterminal>AutoGenLabel_0</Nonterminal> <Eventtype>LEFTCLICK</Eventtype> <Eventvalue>UNKNOWN</Eventvalue> </Component> <Menu> <Window>mynotepad_0</Window> <Nonterminal>File_0</Nonterminal> </Menu> <Menu> <Window>mynotepad_0</Window> <Nonterminal>Search_0</Nonterminal> </Menu> <Menu> <Window>mynotepad_0</Window> <Nonterminal>Close_0</Nonterminal> </Menu> </Testcase> <Testcase> <Test>260</Test> <Length>3</Length> <Component> <Window>mynotepad_0</Window> <Nonterminal>AutoGenLabel_0</Nonterminal> <Eventtype>LEFTCLICK</Eventtype> <Eventvalue>UNKNOWN</Eventvalue> </Component> <Menu> <Window>mynotepad_0</Window> <Nonterminal>File_0</Nonterminal> </Menu> <Menu> <Window>mynotepad_0</Window> <Nonterminal>Help_0</Nonterminal> </Menu> </Testcase> <Testcase> <Test>261</Test> <Length>3</Length> <Component> <Window>mynotepad_0</Window> <Nonterminal>AutoGenLabel_0</Nonterminal> <Eventtype>LEFTCLICK</Eventtype> <Eventvalue>UNKNOWN</Eventvalue> </Component> <Menu> <Window>mynotepad_0</Window> <Nonterminal>Edit_0</Nonterminal> </Menu> <Component> <Window>mynotepad_0</Window> <Nonterminal>AutoText_0</Nonterminal> <Eventtype>SETTEXT</Eventtype> <Eventvalue>UNKNOWN</Eventvalue> </Component> </Testcase> </Testsuite>
Last edited by Fubarable; 04-10-2010 at 06:11 PM. Reason: Moderator Edit: Code tags added
-
Have you looked at using StAX? This may work well for you: The Java(TM) Web Services Tutorial
Note that folks will have an easier time if you posted formatted code and XML data and use code tags (the latter of which I've added to your post above). Please see the links in my signature regarding these tags.
For instance, which is easier to read, your post, or this?
Best of luck.Java Code:<?xml version="1.0" ?> <Testsuite> <Mode>STRUCTURAL</Mode> <Testcase> <Test>256</Test> <Length>3</Length> <Component> <Window>mynotepad_0</Window> <Nonterminal>AutoGenLabel_0</Nonterminal> <Eventtype>LEFTCLICK</Eventtype> <Eventvalue>UNKNOWN</Eventvalue> </Component> <Menu> <!--this type of nodes i want to delete on basis of Close_0 --> <Window>mynotepad_0</Window> <Nonterminal>File_0</Nonterminal> </Menu> <Menu> <Window>mynotepad_0</Window> <Nonterminal>Open_0</Nonterminal> </Menu> </Testcase> <Testcase> <Test>257</Test> <Length>3</Length> <Component> <Window>mynotepad_0</Window> <Nonterminal>AutoGenLabel_0</Nonterminal> <Eventtype>LEFTCLICK</Eventtype> <Eventvalue>UNKNOWN</Eventvalue> </Component> <Menu> <Window>mynotepad_0</Window> <Nonterminal>File_0</Nonterminal> </Menu> <Menu> <Window>mynotepad_0</Window> <Nonterminal>Close_0</Nonterminal> </Menu> </Testcase> <Testcase> <Test>258</Test> <Length>3</Length> <Component> <Window>mynotepad_0</Window> <Nonterminal>AutoGenLabel_0</Nonterminal> <Eventtype>LEFTCLICK</Eventtype> <Eventvalue>UNKNOWN</Eventvalue> </Component> <Menu> <Window>mynotepad_0</Window> <Nonterminal>File_0</Nonterminal> </Menu> <Menu> <Window>mynotepad_0</Window> <Nonterminal>Edit_0</Nonterminal> </Menu> </Testcase> <Testcase> <Test>259</Test> <Length>3</Length> <Component> <Window>mynotepad_0</Window> <Nonterminal>AutoGenLabel_0</Nonterminal> <Eventtype>LEFTCLICK</Eventtype> <Eventvalue>UNKNOWN</Eventvalue> </Component> <Menu> <Window>mynotepad_0</Window> <Nonterminal>File_0</Nonterminal> </Menu> <Menu> <Window>mynotepad_0</Window> <Nonterminal>Search_0</Nonterminal> </Menu> <Menu> <Window>mynotepad_0</Window> <Nonterminal>Close_0</Nonterminal> </Menu> </Testcase> <Testcase> <Test>260</Test> <Length>3</Length> <Component> <Window>mynotepad_0</Window> <Nonterminal>AutoGenLabel_0</Nonterminal> <Eventtype>LEFTCLICK</Eventtype> <Eventvalue>UNKNOWN</Eventvalue> </Component> <Menu> <Window>mynotepad_0</Window> <Nonterminal>File_0</Nonterminal> </Menu> <Menu> <Window>mynotepad_0</Window> <Nonterminal>Help_0</Nonterminal> </Menu> </Testcase> <Testcase> <Test>261</Test> <Length>3</Length> <Component> <Window>mynotepad_0</Window> <Nonterminal>AutoGenLabel_0</Nonterminal> <Eventtype>LEFTCLICK</Eventtype> <Eventvalue>UNKNOWN</Eventvalue> </Component> <Menu> <Window>mynotepad_0</Window> <Nonterminal>Edit_0</Nonterminal> </Menu> <Component> <Window>mynotepad_0</Window> <Nonterminal>AutoText_0</Nonterminal> <Eventtype>SETTEXT</Eventtype> <Eventvalue>UNKNOWN</Eventvalue> </Component> </Testcase> </Testsuite>
Last edited by Fubarable; 04-10-2010 at 07:09 PM.
Similar Threads
-
How to check file existence and rename file with little modification
By Basit56 in forum New To JavaReplies: 5Last Post: 10-17-2012, 10:07 AM -
n00b: jTable gui modification
By ankitmcgill in forum New To JavaReplies: 1Last Post: 03-15-2009, 06:34 PM -
help with concurrent modification exception
By jdgallag in forum New To JavaReplies: 1Last Post: 11-30-2008, 09:19 PM -
How to change the date of modification of a file
By Java Tip in forum java.ioReplies: 0Last Post: 04-05-2008, 10:10 AM -
Inventory Program modification help
By badness in forum Java AppletsReplies: 1Last Post: 01-17-2008, 05:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks