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:
- 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();
- 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.
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.
|