advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : XML Made Easy with XOM :

Serializing an XML with XOM

To serialize an XML document, use the methods in the nu.xom.Serializer class:
  • public Serializer(OutputStream out): This method serializes into an OutputStream in UTF-8 encoding.
  • public Serializer(OutputStream out,String encoding) throws UnsupportedEncodingException: This method serializes into an OutputStream in encoding.
Before serializing, specify a set of options for formatting the serialization results:
  • public void setLineSeparator(String lineSeparator): This method sets the line separator (\n, \r, or \r\n).
  • public void setIndent(int indent): This method sets the number of spaces used to visually separate the document levels.
  • public void setMaxLength(int maxLength): This method ets the maximum length of a line. Setting this to 0 indicates that no automatic wrapping is to be performed.
Here’s a simple example of the serialized AirWings_xml.xml document. :
…
try {
       //Serializer serializer = 
	   new Serializer(System.out, "ISO-8859-1");
       Serializer serializer = 
	   new Serializer(new FileOutputStream(new File(".//AirWings_xml.xml")), 
	   "ISO-8859-1");
       serializer.setIndent(5);
       serializer.setMaxLength(100);
       serializer.write(doc);  
       } catch (IOException e) { System.out.println(e.getMessage()); }  

Parsing and Navigating an XML Document with XOM

Parsing an XML document with XOM is a very simple task that can be accomplished in two steps:
  1. Create an instance of nu.xom.Builder class using one of its constructors. The simplest way is to use the empty constructor, like this:
    Builder builder = new Builder();
    
  2. Call one of the build methods of the Builder class. These methods allow you to process a document from different source like: String, file, network socket, URL, java.io.Reader, and so on. For example, suppose you want to process the AirWings_xml.xml document, which is a file, you can do so like this:
    Document doc = builder.build(new File(".//AirWings_xml.xml"));
    
Now, the document is ready for navigation using some dedicated classes like nu.xom.Document, nu.xom.Element, nu.xom.Elements, nu.xom.Attribute, nu.xom.Node, nu.xom.ParentNode, nu.xom.Nodes, and so on. Here are some of the most used methods for navigation scopes. Their names are very intuitive and provide sufficient information to understand the effect and the desired parameters:
  • Get/set document root:
    public final Element getRootElement()	 -	 nu.xom.Document
    public void setRootElement(Element root) -	 nu.xom.Document
    
  • Get/set document's document type declaration:
    public final DocType getDocType()		 -	 nu.xom.Document
    public void setDocType(DocType doctype)  -	 nu.xom.Document
    
  • Adding/removing/replacing/inserting nodes:
    public void appendChild(Node child)		 -	 nu.xom.ParentNode
    public Node removeChild(Node child)		 -	 nu.xom.Document
    public Node removeChild(int position)	 -	 nu.xom.Document
    public void replaceChild(Node oldChild,Node newChild	-	nu.xom.Document
    public void insertChild(Node child,int position)		-	nu.xom.ParentNode
    
  • Manipulating elements:
    public final Elements getChildElements()	-	nu.xom.Element
    public final Elements getChildElements(String name) -	nu.xom.Element
    public final Elements getChildElements(String ln,String namespaceURI)-	nu.xom.Element
    public final Element getFirstChildElement(String name) 	-	nu.xom.Element
    public final Element getFirstChildElement(String ln,String namespaceURI) -	 nu.xom.Element
    public final String getLocalName()		-	nu.xom.Element
    public final String getQualifiedName()	-	nu.xom.Element
    public void setLocalName(String localName) 	-	nu.xom.Element
    
  • Manipulating attributes:
    public final Attribute getAttribute(int index) 	-	nu.xom.Element
    public final Attribute getAttribute(String name) 	-	nu.xom.Element
    public final Attribute getAttribute(String ln,String namespaceURI)	 -	nu.xom.Element
    public final int getAttributeCount()		-	nu.xom.Element
    public final String getAttributeValue(String name) 	-	nu.xom.Element
    public final String getAttributeValue(String ln,String namespaceURI) -	nu.xom.Element
    public final String getLocalName()		-	nu.xom.Attribute
    public void setLocalName(String localName) 	-	nu.xom.Attribute
    public final String getValue()				-	nu.xom.Attribute
    public final String getQualifiedName()		-	nu.xom.Attribute
    
Listing 3 shows how to parse the AirWings_xml.xml document and how to recursively traverse the document tree for displaying the nodes's names and values.

Home / Articles / XML Made Easy with XOM / 1 / 2 / Next: Validating an XML Document with XOM

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 34
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

IBM Brings Developers Into the Cloud
Apache at 10: You Can't Buy Us
Microsoft's CodePlex Foundation Moving Forward
Apple Claims 100,000 Apps, Google Analyzes Them
Nokia Latest to Play Opera Mobile 10 Browser
PayPal Opens Up Payment Platform to Devs
Ubuntu Linux 9.10 'Karmic Koala' Starts Its Climb
IBM Links Rational Developer Tools, Tivoli Apps
Libraries Give Vista Apps a Windows 7 Look
Ubuntu: The 'Default Alternative' to Windows?

Delivering Web-based Embedded Fonts in CSS 3
Adobe Helps PHP Developers Create Rich Internet Applications
Java Developers Finding a Home at Adobe Flex
Virtualization Delivers a Dynamic Infrastructure
Consuming XML Web Services in iPhone Applications
Build a More Agile Business with IBM
POJO-Based Solutions for LDAP Access: One Good, One Better
IBM Offers Enhanced Measurement and Management for Energy Usage
IBM Helps Transformation to an Information-Based Enterprise
Top Five Touch UI-Related Design Guidelines

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs