Results 1 to 2 of 2
  1. #1
    anurag_1226 is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default 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)

    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> 
    <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>
    Moderator Edit: Code tags added -- although code was not formatted to begin with
    Last edited by Fubarable; 04-10-2010 at 07:11 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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?
    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>
    Best of luck.
    Last edited by Fubarable; 04-10-2010 at 08:09 PM.

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2012, 11:07 AM
  2. n00b: jTable gui modification
    By ankitmcgill in forum New To Java
    Replies: 1
    Last Post: 03-15-2009, 07:34 PM
  3. help with concurrent modification exception
    By jdgallag in forum New To Java
    Replies: 1
    Last Post: 11-30-2008, 10:19 PM
  4. Replies: 0
    Last Post: 04-05-2008, 11:10 AM
  5. Inventory Program modification help
    By badness in forum Java Applets
    Replies: 1
    Last Post: 01-17-2008, 06:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •