Meaningless Drivel is fun!*
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 Android Security Essentials Live Lessons this week in the Android 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: 38075
    
  22
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
 
Similar Threads
how about process large XML file(bigger than 1GB) in Java?
Performance Recommendation for Simple XML Parsing
XML data binding in J2me
validating xml??
where can find XML Parser?