Anurag Gupta
Java XML modification
1 Answer(s)      5 years and a month ago
Posted in : Java Beginners
hey I want to delete the nodes in Xml file & that should be updated 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 static void main(String [] args) {


String str="Close_0";

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
Document doc = null;


try {

db = dbf.newDocumentBuilder();
doc = db.parse("Testsuite.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(str)){


testcase.removeChild(node1);

}
}
}
}

}

}



} catch (ParserConfigurationException e1) {
e1.printStackTrace();
}
catch (SAXException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}



}

}





Here is Testsuite.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>
<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>

</Testcase>
</Testsuite>
Advertisement
View Answers

April 12, 2010 at 12:37 PM


Hi Friend,

Try the following code:

import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class Delete{
static Transformer tFormer;
static DocumentBuilder builder;
static Document document;
public static void main( String[] args ){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
TransformerFactory tFactory = TransformerFactory.newInstance();
try{
tFormer = tFactory.newTransformer();
builder = factory.newDocumentBuilder();
document = builder.parse( new File( "Testsuite.xml" ) );
delNode( document, "Menu" );
} catch ( Exception e ){
e.printStackTrace();
}
}

public static void delNode( Node parent, String filter ){
try{
NodeList children = parent.getChildNodes();

for( int i=0; i < children.getLength(); i++ ){
Node child = children.item( i );

if( child.getNodeType() == Node.ELEMENT_NODE ){

if( child.getNodeName().equals(filter) ){
parent.removeChild( child );
} else {
delNode( child, filter );
}
}
}
Source source = new DOMSource(document);
StreamResult dest = new StreamResult("Testsuite.xml");
tFormer.transform(source, dest);
}
catch(Exception e){}
}

}

Thanks









Related Tutorials/Questions & Answers:
Java XML modification - Java Beginners
Java XML modification  hey I want to delete the nodes in Xml file & that should be updated in xml file.here is what i have tried. import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter
Java XML modification
Java XML modification This section explain you how to modify the xml file..., DocumentBuilder and Document classes to get the default DOM parser and parse the xml... . Here is the data.xml: <?xml version="1.0" encoding
Advertisements
Getting the modification date and time of file or folder in Java
C:\nisha>java GetDirectoryAndFileModifiedTime Enter file or directory name in proper format to get the modification date and time : myfile.txt File name : myfile.txt File modification date
java and xml - XML
java and xml  Hi Deepak, I want learn xml and java(applications). Which editor is best usefull(trial--version) in my applications...; Hi friend, http://www.roseindia.net/xml/dom/ Thanks
Java + XML - XML
Java + XML  1) I have some XML files, read one xml... java...the attribute value..not sure now how to read the xml file passing..." pointing to a hello xml file ..read that file and get the value of the parent
Java-XML-DOM - XML
Java-XML-DOM  Hi! I need some help. I have to make java program that loads an xml file and from it builds DOM(later i will have to work with it - like using xpath in java find some value and replace it...). Since i'm new to java
java and xml - XML
java and xml   test_final_1 2009-025T13:23:45 B2B I want to validate each tag.. and i don't have any xsd.. kindly help me to solve...://www.roseindia.net/xml/dom/DOMValidateDTD.shtml Thanks
XML with JAVA - XML
document and the processing in JAVA program . Find the time for each XML element...XML with JAVA  Hi.. This is Priya here. Thanks for responding me. I have a query. "Write program in java to evaluate the time to access
java and xml problem - XML
java and xml problem  hi, i need to write a java program that generates an xml file as follows: aaa vvv --------- i have witten a program in java, but im
java with xml
java with xml  Hi i am reading xml data with sax parser in java. ok its fine. But in future xsd and xml will change. now my question is if xsd and XML will change my java progrm will not change. is it possible ? Thanks
java with xml
java with xml  Hi i am reading xml data with sax parser in java. ok its fine. But in future xsd and xml will change. now my question is if xsd and XML will change my java progrm will not change. is it possible ? Thanks
XML in java - XML
XML in java  Write a program using SAX that will count the number of occurrences of each element type in an XML document and display them...://www.roseindia.net/xml/sax/SAXElementCount.shtml Hope that it will be helpful
Read XML in java - XML
Read XML in java  Hi Deepak, I want to read a xml file which have only one element with multiple attributes with same tag name. here is my file... a solution.my java code is given below. import java.io.File; import
Session Modification
Session Modification in PHP Session modification can be done through incrementing the loop. As the counting of loop increments, the session be modified.  First of all, begin the session with session_start(),  set the input
java - XML
This is my XML file then how can read this XML file Using DOM & SAX parsers in java? How can write the same data into XML file using DOM parser? Could you
java with xml
java with xml  hi i have a problem. // this sample code is reading xml file in java DefaultHandler handler = new DefaultHandler... xml file change in future then my java code will also change. but is there any
java - XML
java  How can write data into XML file using DOM parser? How can convert HTWL file to XML file
java - XML
java  how can i validate my xml file using java code plz send me de... kumar  Hi friend, Step to validate a xml file against a DTD (Document Type Definition) using the DOM APIs. Here is the xml file "User.xml
XML and java
XML and java  Hi I have xml data in XMLStreamreader object how to retrive tha data and write into a file using java Your help will be much appreciated Thanks   Hi Friend, Try the following code: import java.io.
Modification in conventional ModelAndView
In this section, you will get to known about the new modification in conventional ModelAndView
java - XML
java  How to prepare an xml document by reading data from a table in the database . (the database access logic is written inside the servlet
java - XML
you get the data3. And in the servlet class you can use the DOM api to create xml... it to the xml treedoc.appendChild(root);For more information please visit http://www.roseindia.net/xml/dom/createdomchildelement.shtmlThanks
java - XML
in the XML document while i am parsing the file using SAX event based parser.... provide the way in java.  Hi Friend, Try the following code: import... parser = parserFact.newSAXParser(); System.out.println("XML Data
java - XML
java  How to read the values of XMLStreamConstants.CDATA in simple java
JAVA - XML
JAVA  hi.. i want to talk to any SWT expert in JAVA... how can i do it?   Hi friend, For read more information,Examples and Tutorials on SWT visit to : http://www.roseindia.net/tutorials/swt/ Thanks
XML parsing using Java - XML
XML parsing using Java  I'm trying to parse a big XML file in JAVA. The goal is like i will take console input from user until "Coverage" in element "MIRate"(THE XML code is below).once he enters coverage rate I need
Finding Last Modification Date of a file
Get the Last Modification Date      Get the Last Modification Date This section contains the detail about how to get the last modification date of a file
xml to html via java
xml to html via java  how to read xml into html using java code
Java XML
Java XML In this section we will learn how to manipulate XML files in Java program. Java provides excellent API for manipulating the XML files. In your Java... the XML files. API's for XML parsing in Java: DOM The DOM API
accessing xml using java
accessing xml using java  I need to retrieve some elements in xml file using java   Hi Friend, Please visit the following links: http://www.roseindia.net/xml/Listingnode.shtml http://www.roseindia.net/xml/getting
xml file creation in java
xml file creation in java  how to create xml file in java so...; Please visit the following links: http://www.roseindia.net/tutorial/java/xml...;Please visit the following links: http://www.roseindia.net/tutorial/java/xml