dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Java
Expertise: Intermediate
Jun 19, 2006

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Creating a StAX Filter for XML

This tip creates a StAX filter using the javax.xml.stream.StreamFilter interface. This filter accepts only characters, but you can also set the filter XML to retrieve only elements, attributes, or both:

class BasicStreamFilter implements StreamFilter{

public BasicStreamFilter(){}

//implement the StreamFilter.accept method
public boolean accept(XMLStreamReader XMLsrFilter)
 {
 if(XMLsrFilter.isCharacters())return true; else return false;
 }
}
...


public class StAXBasicStreamFilter{

   public StAXBasicStreamFilter(){}
 
    public static void main(String[] args)
        {
        XMLInputFactory XMLif=null;
        XMLStreamReader XMLsr=null;
        XMLStreamReader XMLsrFilter=null;
              
System.setProperty("javax.xml.stream.XMLInputFactory",
"com.sun.xml.stream.ZephyrParserFactory");
      
//create an XMLInputFactory intance
XMLif=XMLInputFactory.newInstance();
       
//setting a few properties
XMLif.setProperty("javax.xml.stream.isSupportingExternalEntities",Boolean.TRUE);
XMLif.setProperty("javax.xml.stream.isNamespaceAware",Boolean.TRUE);
XMLif.setProperty("javax.xml.stream.isReplacingEntityReferences",Boolean.TRUE);       
                                    
//get an XMLStreamReader object
try{          
   XMLsr=XMLif.createXMLStreamReader   
        ("file:///C:/Data_Local/XML/",new FileReader
             ("C://Data_local//XML//AutoDealer.xml"));
   XMLsrFilter=XMLif.createFilteredReader(XMLsr,new BasicStreamFilter
        ());           
   }catch(java.io.FileNotFoundException e)
        {System.out.println(e.getMessage());
   }catch(javax.xml.stream.XMLStreamException e)    
        {System.out.println(e.getMessage());}
                                          
 ...
 while(XMLsrFilter.hasNext())
      {                       
      int ev=XMLsrFilter.getEventType();
 ...
Leonard Anghel
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date