File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

remove elements from XML file using XPP Parser

 
kiran
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kiran, please check your private messages for an important administrative matter.
 
kiran
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic