• Post Reply Bookmark Topic Watch Topic
  • New Topic

Modification in XML file  RSS feed

 
Anurag Arya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help me i cant move due to this. i want to delete the nodes in xml file.here is what i have tried.





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)


<?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>

<Menu>
<Window>mynotepad_0</Window> // this type of nodes i want to delete on basis of Close_0
<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>
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.

Also, please read TellTheDetails and ItDoesntWorkIsUseless.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Showing all the XML is useless, the logic would be the same no matter what the input document.

1. (assuming as small document) Parse in the document to create a DOM
2. Locate the node you want to delete and its parent
3. Delete the node using the standard API - see org.w3c.dom.Node javadocs
4. Serialize the modified DOM back to a file

How far have you gotten?

Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!