This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes XML and Related Technologies and the fly likes remove elements from XML file using XPP Parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "remove elements from XML file using XPP Parser" Watch "remove elements from XML file using XPP Parser" New topic
Author

remove elements from XML file using XPP Parser

kiran
Greenhorn

Joined: May 14, 2008
Posts: 2
Hi Guys,

I had an XML. I need to remove specified tags in XML, Need to display the remaining content by using XPP parser.
I have done by using DOM, Due to some performence issues, need to do by using XPP parser.

Please suggest me in this. Thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Welcome to the Ranch, and sorry that you haven't got any replies yet.

I think your chances will be better if I move this thread elsewhere, so off we go!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

kiran, please check your private messages for an important administrative matter.
kiran
Greenhorn

Joined: May 14, 2008
Posts: 2
Hi all,

I got the solution finally.
It will take XML as input and will display the remaining content.

import java.io.BufferedReader;
//import java.io.File;
import java.io.FileNotFoundException;
//import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

//import com.thoughtworks.xstream.XStream;

public class ModifyXML
{

public static void main (String args[])
throws XmlPullParserException, IOException
{
StringBuffer sbfinal= new StringBuffer();
String myString=null;
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
StringBuffer buf = new StringBuffer();
try {
BufferedReader in = new BufferedReader(new FileReader("cache_response.xml"));
String line;
while ((line = in.readLine()) != null) {
buf.append(line + System.getProperty("line.separator"));
}
String yourXMLFileInAString = buf.toString();
xpp.setInput(new StringReader(yourXMLFileInAString));
System.out.println("---- >>>>> ");
boolean entered = false;

int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == XmlPullParser.END_DOCUMENT) {
System.out.println("End document");
} else if(eventType == XmlPullParser.START_TAG) {
if(isSkip(xpp.getName()))
entered = true;
else if(!entered){
sbfinal.append("<"+xpp.getName()+">");
}
} else if(eventType == XmlPullParser.END_TAG) {
if(isSkip(xpp.getName()))
entered = false;
else if(!entered){
sbfinal.append("</"+xpp.getName()+">");
}
} else if(eventType == XmlPullParser.TEXT) {
if(!entered){
sbfinal.append(xpp.getText());
}
}
eventType = xpp.next();
}
myString = sbfinal.toString();
System.out.println(myString);
} catch (FileNotFoundException exp) {
exp.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
sbfinal=null;
myString=null;
}
}
public static boolean isSkip(String tagname){
if(tagname.equals("serviceBlock") || tagname.equals("statusBlock") || tagname.equals("____hashCodeCalc"))
return true;
else
return false;
}
}
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"kiran",
Please check your private messages.
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
Don't get me started about those stupid light bulbs.
 
subject: remove elements from XML file using XPP Parser