Java
  Home arrow Java arrow Page 4 - Using XML in a Java Context
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Using XML in a Java Context
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2006-06-22

    Table of Contents:
  • Using XML in a Java Context
  • Modifying an XML Document
  • Formatting an XML Document
  • Evaluating XOM
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using XML in a Java Context - Evaluating XOM


    (Page 4 of 5 )

    These three example applications cover the core features of the main XOM package and are representative of its straightforward approach to XML processing.

    There also are smaller nu.xom.canonical, nu.xom.converters, nu.xom.xinclude, and nu.xom.xslt packages to support XInclude, XSLT, canonical XML serialization, and conversions between the XOM model for XML and the one used by DOM and SAX.

    Listing 20.11 contains an application that works with XML from a dynamic source: RSS feeds of recently updated Web content from the producer of the feed. The RssFilter application searches the feed for specified text in headlines, producing a new XML document that contains only the matching items and shorter indentation. It also modifies the feed's title and adds an RSS 0.91 document type declaration if one is needed.

    Listing 20.11 The Full Text of RssFilter.java

     1: import nu.xom.*;
    2: 
    3: public class RssFilter {
    4:   public static void main(String[] arguments) {
    5: 
    6:     if (arguments.length < 2) {
    7:       System.out.println("Usage: java RssFilter
    rssFile searchTerm"); 8: System.exit(-1); 9: } 10: 11: // Save the RSS location and search term 12: String rssFile = arguments[0]; 13: String searchTerm = arguments[1]; 14: 15: try { 16: // Fill a tree with an RSS file's XML
    data 17: // The file can be local or something on
    the 18: // Web accessible via a URL. 19: Builder bob = new Builder(); 20: Document doc = bob.build(rssFile); 21: 22: // Get the file's root element (<rss>) 23: Element rss = doc.getRootElement(); 24: 25: // Get the element's version attribute 26: Attribute rssVersion =
    rss.getAttribute("version"); 27: String version = rssVersion.getValue(); 28: 29: // Add the DTD for RSS 0.91 feeds, if
    needed 30: if ( (version.equals("0.91")) &
    (doc.getDocType() == null) ) { 31: DocType rssDtd = new DocType("rss", 32: "http://my.netscape.com/publish/
    formats/rss-0.91.dtd"); 33: doc.insertChild(rssDtd, 0); 34: } 35: 36: // Get the first (and only) <channel>
    element 37: Element channel =
    rss.getFirstChildElement("channel"); 38: 39: // Get its <title> element 40: Element title =
    channel.getFirstChildElement("title"); 41: Text titleText = (Text)title.getChild(0); 42: 43: // Change the title to reflect the search
    term 44: titleText.setValue(titleText.getValue() +
    ": Search for " + 45: searchTerm + " articles"); 46: 47: // Get all of the <item> elements and
    loop through them 48: Elements items =
    channel.getChildElements("item"); 49: for (int i = 0; i < items.size(); i++) { 50: // Get an <item> element 51: Element item = items.get(i); 52: 53: // Look for a <title> element inside it 54: Element itemTitle =
    item.getFirstChildElement("title"); 55: 56: // If found, look for its contents 57: if (itemTitle != null) { 58: Text itemTitleText = (Text)
    itemTitle.getChild(0); 59: 60: // If the search text is not found in
    the item, 61: // delete it from the tree 62: if (itemTitleText.toString().indexOf
    (searchTerm) == -1) 63: channel.removeChild(item); 64: } 65: } 66: 67: // Display the results with a serializer 68: Serializer output = new Serializer
    (System.out); 69: output.setIndent(2); 70: output.write(doc); 71: } catch (Exception exc) { 72: System.out.println("Error: " +
    exc.getMessage()); 73: exc.printStackTrace(); 74: } 75: } 76: }

    One feed that can be used to test the application is the one from the author's weblog, Workbench. The following command searches it for items that mention the word "Java":

    java RssFilter http://www.cadenhead.org/workbench/ 
    rss.xml Java

    Comments in the application's source code describe its functionality.

    XOM's design is strongly informed by one overriding principle: enforced simplicity.

    On the Web site for the class library, Harold states that XOM "should help inexperienced developers do the right thing and keep them from doing the wrong thing. The learning curve needs to be really shallow, and that includes not relying on best practices that are known in the community but are not obvious at first glance."

    The new class library is useful for Java programmers whose Java programs require a steady diet of XML.

    More Java Articles
    More By Sams Publishing


       · This article is an excerpt from the book "Sams Teach Yourself Java 2 in 21 Days, 4th...
     

    Buy this book now. This article is excerpted from chapter 20 of the book Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT