Session #2105. 2. JDOM Makes XML Easy. "I think JDOM breaks down a lot of
the barriers between Java and XML and makes it a more natural fit." -- Simon St.
import time class MyThread(threading.Thread): def __init__(self,str): threading.Thread.__init__(self) self.name = str de
exchange in domains ranging from the Web to desktop applications. .... contains the root element of T. Figure 3(a) shows an example of a partitioned XML tree.
exchange in domains ranging from the Web to desktop applications. However, ... DOM is the most popular interface to traverse XML documents because of its ...
Sep 2, 2008 - Cisco Systems. Jyh-Charn ..... John, while the modification scenario calls for switch- ing the first .... Open Platform Software Technology Center.
Jan 13, 2004 ... the pleasures of XML programming is the availability of open-source, no-cost
XML parsers that read XML documents for you. This tutorial ...
Sep 8, 2008 - istics and patterns of duplicate and shared vertices, which is crucial for .... and the MySQL da- tabase, with the Tomcat application server for future use. Fig. .... dedicated to a single pathway, recently have begun to focus on the ..
XML parsing and validation technique that is time and space optimal. ....
description of the scanner is fed to Flex to generate DFA- based XML token
scanner in ...
XML parsing and validation technique that is time and space optimal. ....
description of the scanner is fed to Flex to generate DFA- based XML token
scanner in ...
E.g. the world's leading financial companies have been work- ing on over a dozen ..... gains are remarkable but a standard API is still in development. Optimized ...
Rapid Prototyping with a Local. Geolocation ... Bridges the gap between mobile users and the Web & traditional media. ⢠Allows rapid application development.
Thanks to Erick Breck for many discussions about XParse and to both him and ... [21] Daniel C. Wang, Andrew W. Appel, Je L. Korn, and. Christopher S. Serra.
Claire Grover and Alex Lascarides. Division of Informatics. The University .... 2The LT TTT tagger uses the Penn Treebank tagset (Mar- cus et al., 1994): JJ labels ...
age scenarios enabled by BiM, such as dynamic updates of XML data. From the ..... ios: traditional XML handling and handling dynamic updates of XML data.
fact that XML is based on plain text, the metadata can be easily used by several applications, ranging from ed- ucational services to geographical information ...
tectures (SOAs) [16], grid computing, RSS feeds, ecommerce sites, and most recently ... support to an application, and it is widely supported in open-source and ...
Table 4: Cloud Service Data Rate. CDN Service. Cloud Storage Service. Provider. Rate (Mb/s). Provider. Rate (Mb/s). Akamai CDN. 27.50. Amazon S3 - US East.
Page: 1. Parsing RDF documents using PHP. "An introduction to the RDF XML
syntax and how to parse RDF documents using the PHP version of Repat.".
Technology. Robert D. ... 1 Introduction. Parallel bit stream technology offers a promis- ...... Bachelor of Science in Computing Science with a Certificate in Spatial ...
A general action is a semicolon-separated list of commands terminated by an expression. Actions can also be error(msg), which signals a parse error, ...
Parallel bit stream technology offers a promis- ing way to break out of the performance bottle- neck associated with traditional byte-at-a-time text processing on ...
Permission to make digital or hard copies of all or part of this work for personal .... ') according to XMLSchema id bib.xsd)). The XML storage system remembers the type annotation derived ..... of early System R [35] â we currently we choose plans
similar to the World Wide Web Consortium's (W3C). DOM, it's an ... good part of this design is that as SAX parsers get f
JDOM and XML Parsing, Part 1 JDOM makes XML manipulation in Java easier than ever.
THE JDOM PACKAGE STRUCTURE
The JDOM library consists of six packages. First, the org.jdom package holds the classes representing an XML document and its components: Attribute, C border="0"> ...
With a reference to an element, you can ask the element for any named attribute value: String val = table.getAttributeValue("width");
You can also get the attribute as an object, for performing special manipulations such as type conversions: Attribute border = table.getAttribute("border"); int size = border.getIntValue();
Using the List metaphor makes possible many element manipulations without adding a plethora of methods. For convenience, however, the common tasks of adding elements at the end or removing named elements have methods on Element itself and don’t require obtaining the List first:
To set or change an attribute, use setAttribute(): table.setAttribute("vspace", "0");
To remove an attribute, use removeAttribute(): table.removeAttribute("vspace");
root.removeChildren("jill");
WORKING WITH ELEMENT TEXT CONTENT
root.addContent(new Element("jenny"));
An element with text content looks like this: One nice perk with JDOM is how easy it can be to move elements within a document or between documents. It’s the same code in both cases:
A cool demo
Element movable = new Element("movable"); parent1.addContent(movable);
// place
In JDOM, the text is directly available by calling:
If you really want whitespace out of the picture, there’s even a getTextNormalize() method that normalizes internal whitespace
Some text
Some child element
When an element contains both text and child elements, it’s said to contain “mixed content.” Handling mixed content can be potentially difficult, but JDOM makes it easy. The standard-use cases—retrieving text content and navigating child elements—are kept simple:
with a single space. It’s handy for text content like this:
String text = table.getTextTrim();
// "Some text"
Element tr = table.getChild("tr");
// A straight reference
Sometimes you have text content with formatting space within the string.
For more advanced uses needing the comment, whitespace blocks, processing instructions, and entity references, the raw mixed content is available as a List:
To change text content, there’s a setText() method: List mixedCo = table.getContent(); description.setText("A new description");
Iterator itr = mixedCo.iterator(); while (itr.hasNext()) {
Any special characters within the text will be interpreted correctly as a character and escaped on output as needed to maintain the appropriate semantics. Let’s say you make this call:
Object o = i.next(); if (o instanceof Comment) { ... }
element.setText(" content");
// Types include Comment, Element, CDATA, DocType, // ProcessingInstruction, EntityRef, and Text
The internal store will keep that literal string as characters. There will be no implicit parsing of the content. On output, you’ll see this:
}
As with child element lists, changes to the raw content list affect the backing document:
content // Remove the Comment.
This behavior preserves the semantic meaning of the earlier setText() call. If you want XML content held within an element, you must add the appropriate JDOM child element objects. Handling CDATA sections is also possible within JDOM. A CDATA section indicates a block of text that shouldn’t be parsed. It is essentially a “syntactic sugar” that allows the easy inclusion of HTML or XML content without so many < and > escapes. To build a CDATA section, just wrap the string with a CDATA object: element.addContent(new CDATA(" content"));
What’s terrific about JDOM is that a getText() call returns the string of characters without bothering the caller with whether or not it’s represented by a CDATA section. DEALING WITH MIXED CONTENT
Some elements contain many things such as whitespace, comments, text, child elements, and more:
It's "1" because "0" is a whitespace block.
mixedCo.remove(1);
If you have sharp eyes, you’ll notice that there’s a Text class here. Internally, JDOM uses a Text class to store string content in order to allow the string to have parentage and more easily support XPath access. As a programmer, you don’t need to worry about the class when retrieving or setting text—only when accessing the raw content list. For details on the DocType, ProcessingInstruction, and EntityRef classes, see the API documentation at www.jdom.org. COMING IN PART 2
In this article we began examining how to use JDOM in your applications. In the next article, I examine XML Namespaces, ResultSetBuilder, XSLT, and XPath. You can find Part 2 of this series online now at otn.oracle.com/oraclemagazine. ■ Jason Hunter ([email protected]) is a consultant, publisher of Servlets.com, and vice president of the Apache Software Foundation. He holds a seat on the JCP Executive Committee.
ORACLE MAGAZINE
SEPTEMBER/OCTOBER 2002
73
DEVELOPER
String betterDesc = description.getTextTrim();
ORACLE
Just remember, because the XML 1.0 specification requires whitespace to be preserved, this returns "\n A cool demo\n". Of course, as a practical programmer you often don’t need want to be so literal about formatting whitespace, so there’s a convenient method for retrieving the text while ignoring surrounding whitespace: