Free Essay

Ssss

In:

Submitted By sunilsuryant
Words 13605
Pages 55
Tutorial: XML programming in Java Doug Tidwell
Cyber Evangelist, developerWorks XML Team September 1999

About this tutorial
Our first tutorial, “Introduction to XML,” discussed the basics of XML and demonstrated its potential to revolutionize the Web. This tutorial shows you how to use an XML parser and other tools to create, process, and manipulate XML documents. Best of all, every tool discussed here is freely available at IBM’s alphaWorks site (www.alphaworks.ibm.com) and other places on the Web.

About the author
Doug Tidwell is a Senior Programmer at IBM. He has well over a seventh of a century of programming experience and has been working with XML-like applications for several years. His job as a Cyber Evangelist is basically to look busy, and to help customers evaluate and implement XML technology. Using a specially designed pair of zircon-encrusted tweezers, he holds a Masters Degree in Computer Science from Vanderbilt University and a Bachelors Degree in English from the University of Georgia.

1

Section 1 – Introduction

Tutorial – XML Programming in Java

Section 1 – Introduction
About this tutorial Our previous tutorial discussed the basics of XML and demonstrated its potential to revolutionize the Web. In this tutorial, we’ll discuss how to use an XML parser to: • • • Process an XML document Create an XML document Manipulate an XML document

We’ll also talk about some useful, lesser-known features of XML parsers. Best of all, every tool discussed here is freely available at IBM’s alphaWorks site (www.alphaworks.ibm.com) and other places on the Web.

What’s not here There are several important programming topics not discussed here: • • • Using visual tools to build XML applications Transforming an XML document from one vocabulary to another Creating interfaces for end users or other processes, and creating interfaces to back-end data stores

All of these topics are important when you’re building an XML application. We’re working on new tutorials that will give these subjects their due, so watch this space!

XML application architecture
User Interface XML Application XML Parser (Original artwork drawn by Doug Tidwell. All rights reserved.) Data Store

An XML application is typically built around an XML parser. It has an interface to its users, and an interface to some sort of back-end data store. This tutorial focuses on writing Java code that uses an XML parser to manipulate XML documents. In the beautiful picture on the left, this tutorial is focused on the middle box.

2

Tutorial – XML Programming in Java

Section 2 – Parser basics

Section 2 – Parser basics

The basics An XML parser is a piece of code that reads a document and analyzes its structure. In this section, we’ll discuss how to use an XML parser to read an XML document. We’ll also discuss the different types of parsers and when you might want to use them. Later sections of the tutorial will discuss what you’ll get back from the parser and how to use those results.

How to use a parser We’ll talk about this in more detail in the following sections, but in general, here’s how you use a parser: 1. Create a parser object 2. Pass your XML document to the parser 3. Process the results Building an XML application is obviously more involved than this, but this is the typical flow of an XML application.

Kinds of parsers There are several different ways to categorize parsers: • • • • Validating versus non-validating parsers Parsers that support the Document Object Model (DOM) Parsers that support the Simple API for XML (SAX) Parsers written in a particular language (Java, C++, Perl, etc.)

3

Section 2 – Parser basics

Tutorial – XML Programming in Java Validating versus non-validating parsers As we mentioned in our first tutorial, XML documents that use a DTD and follow the rules defined in that DTD are called valid documents. XML documents that follow the basic tagging rules are called well-formed documents. The XML specification requires all parsers to report errors when they find that a document is not wellformed. Validation, however, is a different issue. Validating parsers validate XML documents as they parse them. Non-validating parsers ignore any validation errors. In other words, if an XML document is well-formed, a non-validating parser doesn’t care if the document follows the rules specified in its DTD (if any).

Why use a non-validating parser? Speed and efficiency. It takes a significant amount of effort for an XML parser to process a DTD and make sure that every element in an XML document follows the rules of the DTD. If you’re sure that an XML document is valid (maybe it was generated by a trusted source), there’s no point in validating it again. Also, there may be times when all you care about is finding the XML tags in a document. Once you have the tags, you can extract the data from them and process it in some way. If that’s all you need to do, a non-validating parser is the right choice.

The Document Object Model (DOM) The Document Object Model is an official recommendation of the World Wide Web Consortium (W3C). It defines an interface that enables programs to access and update the style, structure, and contents of XML documents. XML parsers that support the DOM implement that interface. The first version of the specification, DOM Level 1, is available at http://www.w3.org/TR/REC-DOMLevel-1, if you enjoy reading that kind of thing.

4

Tutorial – XML Programming in Java

Section 2 – Parser basics What you get from a DOM parser When you parse an XML document with a DOM parser, you get back a tree structure that contains all of the elements of your document. The DOM provides a variety of functions you can use to examine the contents and structure of the document.

A word about standards Now that we’re getting into developing XML applications, we might as well mention the XML specification. Officially, XML is a trademark of MIT and a product of the World Wide Web Consortium (W3C). The XML Specification, an official recommendation of the W3C, is available at www.w3.org/TR/RECxml for your reading pleasure. The W3C site contains specifications for XML, DOM, and literally dozens of other XML-related standards. The XML zone at developerWorks has an overview of these standards, complete with links to the actual specifications.

The Simple API for XML (SAX) The SAX API is an alternate way of working with the contents of XML documents. A de facto standard, it was developed by David Megginson and other members of the XML-Dev mailing list. To see the complete SAX standard, check out www.megginson.com/SAX/. To subscribe to the XML-Dev mailing list, send a message to majordomo@ic.ac.uk containing the following: subscribe xml-dev.

5

Section 2 – Parser basics

Tutorial – XML Programming in Java What you get from a SAX parser When you parse an XML document with a SAX parser, the parser generates events at various points in your document. It’s up to you to decide what to do with each of those events. A SAX parser generates events at the start and end of a document, at the start and end of an element, when it finds characters inside an element, and at several other points. You write the Java code that handles each event, and you decide what to do with the information you get from the parser.

Why use SAX? Why use DOM? We’ll talk about this in more detail later, but in general, you should use a DOM parser when: • • • You need to know a lot about the structure of a document You need to move parts of the document around (you might want to sort certain elements, for example) You need to use the information in the document more than once

Use a SAX parser if you only need to extract a few elements from an XML document. SAX parsers are also appropriate if you don’t have much memory to work with, or if you’re only going to use the information in the document once (as opposed to parsing the information once, then using it many times later).

6

Tutorial – XML Programming in Java

Section 2 – Parser basics XML parsers in different languages XML parsers and libraries exist for most languages used on the Web, including Java, C++, Perl, and Python. The next panel has links to XML parsers from IBM and other vendors. Most of the examples in this tutorial deal with IBM’s XML4J parser. All of the code we’ll discuss in this tutorial uses standard interfaces. In the final section of this tutorial, though, we’ll show you how easy it is to write code that uses another parser.

Resources – XML parsers Java • IBM’s parser, XML4J, is available at www.alphaWorks.ibm.com/tech/xml4j. • James Clark’s parser, XP, is available at www.jclark.com/xml/xp. • Sun’s XML parser can be downloaded from

developer.java.sun.com/developer/products/xml/



(you must be a member of the Java Developer Connection to download) DataChannel’s XJParser is available at xdev.datachannel.com/downloads/xjparser/.

C++ • IBM’s XML4C parser is available at www.alphaWorks.ibm.com/tech/xml4c. • James Clark’s C++ parser, expat, is available at www.jclark.com/xml/expat.html. Perl • There are several XML parsers for Perl. For more information, see www.perlxml.com/faq/perl-xml-faq.html. Python • For information on parsing XML documents in Python, see www.python.org/topics/xml/.

7

Section 2 – Parser basics

Tutorial – XML Programming in Java One more thing While we’re talking about resources, there’s one more thing: the best book on XML and Java (in our humble opinion, anyway). We highly recommend XML and Java: Developing Web Applications, written by Hiroshi Maruyama, Kent Tamura, and Naohiko Uramoto, the three original authors of IBM’s XML4J parser. Published by Addison-Wesley, it’s available at bookpool.com or your local bookseller.

Summary The heart of any XML application is an XML parser. To process an XML document, your application will create a parser object, pass it an XML document, then process the results that come back from the parser object. We’ve discussed the different kinds of XML parsers, and why you might want to use each one. We categorized parsers in several ways: • • • • Validating versus non-validating parsers Parsers that support the Document Object Model (DOM) Parsers that support the Simple API for XML (SAX) Parsers written in a particular language (Java, C++, Perl, etc.)

In our next section, we’ll talk about DOM parsers and how to use them.

8

Tutorial – XML Programming in Java

Section 3 – The Document Object Model (DOM)

Section 3 – The Document Object Model (DOM)
Dom, dom, dom, dom, dom, Doobie-doobie, Dom, dom, dom, dom, dom… The DOM is a common interface for manipulating document structures. One of its design goals is that Java code written for one DOM-compliant parser should run on any other DOM-compliant parser without changes. (We’ll demonstrate this later.) As we mentioned earlier, a DOM parser returns a tree structure that represents your entire document.

Sample code Before we go any further, make sure you’ve downloaded our sample XML applications onto your machine. Unzip the file xmljava.zip, and you’re ready to go! (Be sure to remember where you put the file.)

DOM interfaces The DOM defines several Java interfaces. Here are the most common: • • • • • Node: The base datatype of the DOM. Element: The vast majority of the objects you’ll deal with are Elements. Attr: Represents an attribute of an element. Text: The actual content of an Element or Attr. Document: Represents the entire XML document. A Document object is often referred to as a DOM tree.

9

Section 3 – The Document Object Model (DOM)

Tutorial – XML Programming in Java Common DOM methods When you’re working with the DOM, there are several methods you’ll use often: • • • Document.getDocumentElement() Returns the root element of the document. Node.getFirstChild() and Node.getLastChild() Returns the first or last child of a given Node. Node.getNextSibling() and Node.getPreviousSibling() Deletes everything in the DOM tree, reformats your hard disk, and sends an obscene e-mail greeting to everyone in your address book. (Not really. These methods return the next or previous sibling of a given Node.) Node.getAttribute(attrName) For a given Node, returns the attribute with the requested name. For example, if you want the Attr object for the attribute named id, use getAttribute("id").



Our first DOM application!
<?xml version="1.0"?> <sonnet type="Shakespearean"> <author> <last-name>Shakespeare</last-name> <first-name>William</first-name> <nationality>British</nationality> <year-of-birth>1564</year-of-birth> <year-of-death>1616</year-of-death> </author> <title>Sonnet 130</title> <lines> <line>My mistress’ eyes are ...

We’ve been at this a while, so let’s go ahead and actually do something. Our first application simply reads an XML document and writes the document’s contents to standard output. At a command prompt, run this command: java domOne sonnet.xml This loads our application and tells it to parse the file sonnet.xml. If everything goes well, you’ll see the contents of the XML document written out to standard output. The domOne.java source code is on page 33.

10

Tutorial – XML Programming in Java

Section 3 – The Document Object Model (DOM) domOne to Watch Over Me The source code for domOne is pretty straightforward. We create a new class called domOne; that class has two methods, parseAndPrint and printDOMTree. In the main method, we process the command line, create a domOne object, and pass the file name to the domOne object. The domOne object creates a parser object, parses the document, then processes the DOM tree (aka the Document object) via the printDOMTree method. We’ll go over each of these steps in detail.

public class domOne { public void parseAndPrint(String uri) ... public void printDOMTree(Node node) ... public static void main(String argv[]) ...

Process the command line public static void main(String argv[]) { if (argv.length == 0) { System.out.println("Usage: ... "); ... System.exit(1); } domOne d1 = new domOne(); d1.parseAndPrint(argv[0]); }

The code to process the command line is on the left. We check to see if the user entered anything on the command line. If not, we print a usage note and exit; otherwise, we assume the first thing on the command line (argv[0], in Java syntax) is the name of the document. We ignore anything else the user might have entered on the command line. We’re using command line options here to simplify our examples. In most cases, an XML application would be built with servlets, Java Beans, and other types of components; and command line options wouldn’t be an issue.

Create a domOne object public static void main(String argv[]) { if (argv.length == 0) { System.out.println("Usage: ... "); ... System.exit(1); } domOne d1 = new domOne(); d1.parseAndPrint(argv[0]); }

In our sample code, we create a separate class called domOne. To parse the file and print the results, we create a new instance of the domOne class, then tell our newly-created domOne object to parse and print the XML document. Why do we do this? Because we want to use a recursive function to go through the DOM tree and print out the results. We can’t do this easily in a static method such as main, so we created a separate class to handle it for us.

11

Section 3 – The Document Object Model (DOM)

Tutorial – XML Programming in Java Create a parser object Now that we’ve asked our instance of domOne to parse and process our XML document, its first order of business is to create a new Parser object. In this case, we’re using a DOMParser object, a Java class that implements the DOM interfaces. There are other parser objects in the XML4J package, such as SAXParser, ValidatingSAXParser, and NonValidatingDOMParser. Notice that we put this code inside a try block. The parser throws an exception under a number of circumstances, including an invalid URI, a DTD that can’t be found, or an XML document that isn’t valid or well-formed. To handle this gracefully, we’ll need to catch the exception.

try { DOMParser parser = new DOMParser(); parser.parse(uri); doc = parser.getDocument(); }

try { DOMParser parser = new DOMParser(); parser.parse(uri); doc = parser.getDocument(); } ... if (doc != null) printDOMTree(doc);

Parse the XML document Parsing the document is done with a single line of code. When the parse is done, we get the Document object created by the parser. If the Document object is not null (it will be null if something went wrong during parsing), we pass it to the printDOMTree method.

public void printDOMTree(Node node) { int nodeType = Node.getNodeType(); switch (nodeType) { case DOCUMENT_NODE: printDOMTree(((Document)node). GetDocumentElement()); ... case ELEMENT_NODE: ... NodeList children = node.getChildNodes(); if (children != null) { for (int i = 0; i < children.getLength(); i++) printDOMTree(children.item(i); }

Process the DOM tree Now that parsing is done, we’ll go through the DOM tree. Notice that this code is recursive. For each node, we process the node itself, then we call the printDOMTree function recursively for each of the node’s children. The recursive calls are shown at left. Keep in mind that while some XML documents are very large, they don’t tend to have many levels of tags. An XML document for the Manhattan phone book, for example, might have a million entries, but the tags probably wouldn’t go more than a few layers deep. For this reason, stack overflow isn’t a concern, as it is with other recursive algorithms.

12

Tutorial – XML Programming in Java

Section 3 – The Document Object Model (DOM) Nodes a-plenty

Document Statistics for sonnet.xml: ==================================== Document Nodes: 1 Element Nodes: 23 Entity Reference Nodes: 0 CDATA Sections: 0 Text Nodes: 45 Processing Instructions: 0 ---------Total: 69 Nodes

If you look at sonnet.xml, there are twenty-four tags. You might think that would translate to twenty-four nodes. However, that’s not the case. There are actually 69 nodes in sonnet.xml; one document node, 23 element nodes, and 45 text nodes. We ran java domCounter sonnet.xml to get the results shown on the left. The domCounter.java source code is on page 35.

Sample node listing For the fragment on the left, here are the nodes returned by the parser: 1. The Document node 2. The Element node corresponding to the <sonnet> tag 3. A Text node containing the carriage return at the end of the <sonnet> tag and the two spaces in front of the <author> tag 4. The Element node corresponding to the <author> tag 5. A Text node containing the carriage return at the end of the <author> tag and the four spaces in front of the <last-name> tag 6. The Element node corresponding to the <last-name> tag 7. A Text node containing the characters “Shakespeare” If you look at all the blank spaces between tags, you can see why we get so many more nodes than you might expect.

<?xml version="1.0"?>
<!DOCTYPE sonnet SYSTEM "sonnet.dtd"> <sonnet type="Shakespearean"> <author> <last-name>Shakespeare</last-name>

13

Section 3 – The Document Object Model (DOM)

Tutorial – XML Programming in Java All those text nodes If you go through a detailed listing of all the nodes returned by the parser, you’ll find that a lot of them are pretty useless. All of the blank spaces at the start of the lines at the left are Text nodes that contain ignorable whitespace characters. Notice that we wouldn’t get these useless nodes if we had run all the tags together in a single line. We added the line breaks and spaces to our example to make it easier to read. If human readability isn’t necessary when you’re building an XML document, leave out the line breaks and spaces. That makes your document smaller, and the machine processing your document doesn’t have to build all those useless nodes.

<sonnet type="Shakespearean"> <author> <last-name>Shakespeare</last-name> <first-name>William</first-name> <nationality>British</nationality> <year-of-birth>1564</year-of-birth> <year-of-death>1616</year-of-death> </author> <title>Sonnet 130</title> <lines> <line>My mistress' eyes are nothing like the sun,</line>

Know your Nodes switch { case ... case ... case ... } (nodeType) Node.DOCUMENT_NODE: Node.ELEMENT_NODE: Node.TEXT_NODE:

The final point we’ll make is that in working with the Nodes in the DOM tree, we have to check the type of each Node before we work with it. Certain methods, such as getAttributes, return null for some node types. If you don’t check the node type, you’ll get unexpected results (at best) and exceptions (at worst). The switch statement shown here is common in code that uses a DOM parser.

14

Tutorial – XML Programming in Java

Section 3 – The Document Object Model (DOM) Summary Believe it or not, that’s about all you need to know to work with DOM objects. Our domOne code did several things: • • • Created a Parser object Gave the Parser an XML document to parse Took the Document object from the Parser and examined it

In the final section of this tutorial, we’ll discuss how to build a DOM tree without an XML source file, and show you how to sort elements in an XML document. Those topics build on the basics we’ve covered here. Before we move on to those advanced topics, we’ll take a closer look at the SAX API. We’ll go through a set of examples similar to the ones in this section, illustrating the differences between SAX and DOM.

15

Section 4 – The Simple API for XML (SAX)

Tutorial – XML Programming in Java

Section 4 – The Simple API for XML (SAX)
The Simple API for XML SAX is an event-driven API for parsing XML documents. In our DOM parsing examples, we sent the XML document to the parser, the parser processed the complete document, then we got a Document object representing our document. In the SAX model, we send our XML document to the parser, and the parser notifies us when certain events happen. It’s up to us to decide what we want to do with those events; if we ignore them, the information in the event is discarded.

Sample code Before we go any further, make sure you’ve downloaded our sample XML applications onto your machine. Unzip the file xmljava.zip, and you’re ready to go! (Be sure to remember where you put the file.)

SAX events The SAX API defines a number of events. You can write Java code that handles all of the events you care about. If you don’t care about a certain type of event, you don’t have to write any code at all. Just ignore the event, and the parser will discard it.

16

Tutorial – XML Programming in Java

Section 4 – The Simple API for XML (SAX) A wee listing of SAX events We’ll list most of the SAX events here and on the next panel. All of the events on this panel are commonly used; the events on the next panel are more esoteric. They’re part of the HandlerBase class in the org.xml.sax package. • • • startDocument Signals the start of the document. endDocument Signals the end of the document. startElement Signals the start of an element. The parser fires this event when all of the contents of the opening tag have been processed. That includes the name of the tag and any attributes it might have. endElement Signals the end of an element. characters Contains character data, similar to a DOM Text node.

• •

More SAX events Here are some other SAX events: • ignorableWhitespace This event is analogous to the useless DOM nodes we discussed earlier. One benefit of this event is that it’s different from the character event; if you don’t care about whitespace, you can ignore all whitespace nodes by ignoring this event. warning, error, and fatalError These three events indicate parsing errors. You can respond to them as you wish. setDocumentLocator The parser sends you this event to allow you to store a SAX Locator object. The Locator object can be used to find out exactly where in the document an event occurred.

• •

17

Section 4 – The Simple API for XML (SAX)

Tutorial – XML Programming in Java A note about SAX interfaces The SAX API actually defines four interfaces for handling events: EntityHandler, DTDHandler, DocumentHandler, and ErrorHandler. All of these interfaces are implemented by HandlerBase. Most of the time, your Java code will extend the HandlerBase class. If you want to subdivide the functions of your code (maybe you’ve got a great DTDHandler class already written), you can implement the xxxHandler classes individually.

Our first SAX application!
<?xml version="1.0"?> <sonnet type="Shakespearean"> <author> <last-name>Shakespeare</last-name> <first-name>William</first-name> <nationality>British</nationality> <year-of-birth>1564</year-of-birth> <year-of-death>1616</year-of-death> </author> <title>Sonnet 130</title> <lines> <line>My mistress’ eyes are ...

Let’s run our first SAX application. This application is similar to domOne, except it uses the SAX API instead of DOM. At a command prompt, run this command: java saxOne sonnet.xml This loads our application and tells it to parse the file sonnet.xml. If everything goes well, you’ll see the contents of the XML document written out to the console. The saxOne.java source code is on page 37.

public class saxOne extends HandlerBase ... public void startDocument() ... public void startElement(String name, AttributeList attrs) ... public void characters(char ch[], int start, int length)

saxOne overview The structure of saxOne is different from domOne in several important ways. First of all, saxOne extends the HandlerBase class. Secondly, saxOne has a number of methods, each of which corresponds to a particular SAX event. This simplifies our code because each type of event is completely handled by each method.

18

Tutorial – XML Programming in Java

Section 4 – The Simple API for XML (SAX) SAX method signatures

public void startDocument() ... public void startElement(String name, AttributeList attrs) ... public void characters(char ch[], int start, int length) ... public void ignorableWhitespace(char ch[], int start, int length) ... public void endElement(String name) ... public void endDocument() ... public void warning(SAXParseException ex) ... public void error(SAXParseException ex) ... public void fatalError(SAXParseException ex) throws SAXException ...

When you’re extending the various SAX methods that handle SAX events, you need to use the correct method signature. Here are the signatures for the most common methods: • • startDocument() and endDocument() These methods have no arguments. startElement(String name, AttributeList attrs) name is the name of the element that just started, and attrs contains all of the element’s attributes. endElement(String name) name is the name of the element that just ended. characters(char ch[], int start, int length) ch is an array of characters, start is the position in the array of the first character in this event, and length is the number of characters for this event.

• •

public static void main(String argv[]) { if (argv.length == 0) { System.out.println("Usage: ..."); ... System.exit(1); } saxOne s1 = new saxOne(); s1.parseURI(argv[0]); }

Process the command line As in domOne, we check to see if the user entered anything on the command line. If not, we print a usage note and exit; otherwise, we assume the first thing on the command line is the name of the XML document. We ignore anything else the user might have entered on the command line.

public static void main(String argv[]) { if (argv.length == 0) { System.out.println("Usage: ..."); ... System.exit(1); } saxOne s1 = new saxOne(); s1.parseURI(argv[0]); }

Create a saxOne object In our sample code, we create a separate class called saxOne. The main procedure creates an instance of this class and uses it to parse our XML document. Because saxOne extends the HandlerBase class, we can use saxOne as an event handler for a SAX parser.

19

Section 4 – The Simple API for XML (SAX)

Tutorial – XML Programming in Java Create a Parser object

SAXParser parser = new SAXParser(); parser.setDocumentHandler(this); parser.setErrorHandler(this); try { parser.parse(uri); }

Now that we’ve asked our instance of saxOne to parse and process our XML document, it first creates a new Parser object. In this sample, we use the SAXParser class instead of DOMParser. Notice that we call two more methods, setDocumentHandler and setErrorHandler, before we attempt to parse our document. These functions tell our newly-created SAXParser to use saxOne to handle events.

SAXParser parser = new SAXParser(); parser.setDocumentHandler(this); parser.setErrorHandler(this); try { parser.parse(uri); }

Parse the XML document Once our SAXParser object is set up, it takes a single line of code to process our document. As with domOne, we put the parse statement inside a try block so we can catch any errors that occur.

Process SAX events As the SAXParser object parses our document, it calls our implementations of the SAX event handlers as the various SAX events occur. Because saxOne merely writes the XML document back out to the console, each event handler writes the appropriate information to System.out. For startElement events, we write out the XML syntax of the original tag. For character events, we write the characters out to the screen. For ignorableWhitespace events, we write those characters out to the screen as well; this ensures that any line breaks or spaces in the original document will appear in the printed version.

public void startDocument() ... public void startElement(String name, AttributeList attrs) ... public void characters(char ch[], int start, int length) ... public void ignorableWhitespace(char ch[], int start, int length) ...

20

Tutorial – XML Programming in Java Document Statistics for sonnet.xml: ==================================== DocumentHandler Events: startDocument 1 endDocument 1 startElement 23 endElement 23 processingInstruction 0 character 20 ignorableWhitespace 25 ErrorHandler Events: warning 0 error 0 fatalError 0 ---------Total: 93 Events

Section 4 – The Simple API for XML (SAX) A cavalcade of ignorable events As with the DOM, the SAX interface returns more events than you might think. We generated the listing at the left by running java saxCounter sonnet.xml. One advantage of the SAX interface is that the 25 ignorableWhitespace events are simply ignored. We don’t have to write code to handle those events, and we don’t have to waste our time discarding them. The saxCounter.java source code is on page 41.

Sample event listing For the fragment on the left, here are the events returned by the parser: 1. A startDocument event 2. A startElement event for the <sonnet> element 3. An ignorableWhitespace event for the line break and the two blank spaces in front of the <author> tag 4. A startElement event for the <author> element 5. An ignorableWhitespace event for the line break and the four blank spaces in front of the <last-name> tag 6. A startElement event for the <last-name> tag 7. A character event for the characters “Shakespeare” 8. An endElement event for the <last-name> tag

<?xml version="1.0"?>
<!DOCTYPE sonnet SYSTEM "sonnet.dtd"> <sonnet type="Shakespearean"> <author> <last-name>Shakespeare</last-name>

21

Section 4 – The Simple API for XML (SAX)

Tutorial – XML Programming in Java SAX versus DOM – part one

... <book id="1"> <verse> Sing, O goddess, the anger of Achilles son of Peleus, that brought countless ills upon the Achaeans. Many a brave soul did it send hurrying down to Hades, and many a hero did it yield a prey to dogs and vultures, for so were the counsels of Jove fulfilled from the day on which the son of Atreus, king of men, and great Achilles, first fell out with one another. </verse> <verse> And which of the gods was it that set them on to quarrel? It was the son of Jove and Leto; for he was angry with the king and sent a pestilence upon ...

To illustrate the SAX API, we’ve taken our original domOne program and rewritten it to use SAX. To get an idea of the differences between the two, we’ll talk about two parsing tasks. For our first example, to parse The Iliad for all verses that contain the name “Agamemnon,” the SAX API would be much more efficient. We would look for startElement events for the <verse> element, then look at each character event. We would save the character data from any event that contained the name “Agamemnon,” and discard the rest. Doing this with the DOM would require us to build Java objects to represent every part of the document, store those in a DOM tree, then search the DOM tree for <verse> elements that contained the desired text. This would take a lot of memory, and most of the objects created by the parser would be discarded without ever being used.

SAX versus DOM – part two
... <address> <name> <title>Mrs.</title> <first-name>Mary</first-name> <last-name>McGoon</last-name> </name> <street>1401 Main Street</street> <city>Anytown</city> <state>NC</state> <zip>34829</zip> </address> <address> <name> ...

On the other hand, if we were parsing an XML document containing 10,000 addresses, and we wanted to sort them by last name, using the SAX API would make life very difficult for us. We would have to build a data structure that stored every character and startElement event that occurred. Once we built all of these elements, we would have to sort them, then write a method that output the names in order. Using the DOM API instead would save us a lot of time. DOM would automatically store all of the data, and we could use DOM functions to move the nodes in the DOM tree.

22

Tutorial – XML Programming in Java

Section 4 – The Simple API for XML (SAX) Summary At this point, we’ve covered the two major APIs for working with XML documents. We’ve also discussed when you might want to use each one. In our final topic, we’ll discuss some advanced parser functions that you might need as you build an XML application.

23

Section 5 – Advanced parser functions

Tutorial – XML Programming in Java

Section 5 – Advanced parser functions
Overview We’ve covered the basics of using an XML parser to process XML documents. In this section, we’ll cover a couple of advanced topics. First, we’ll build a DOM tree from scratch. In other words, we’ll create a Document object without using an XML source file. Secondly, we’ll show you how to use a parser to process an XML document contained in a string. Next, we’ll show you how to manipulate a DOM tree. We’ll take our sample XML document and sort the lines of the sonnet. Finally, we’ll illustrate how using standard interfaces like DOM and SAX makes it easy to change parsers. We’ll show you versions of two of our sample applications that use different XML parsers. None of the DOM and SAX code changes.

Building a DOM tree from scratch There may be times when you want to build a DOM tree from scratch. To do this, you create a Document object, then add various Nodes to it. You can run java domBuilder to see an example application that builds a DOM tree from scratch. This application recreates the DOM tree built by the original parse of sonnet.xml (with the exception that it doesn’t create whitespace nodes). We begin by creating an instance of the DocumentImpl class. This class implements the Document interface defined in the DOM. The domBuilder.java source code is on page 44.

Document doc = (Document)Class. forName("com.ibm.xml.dom.DocumentImpl"). newInstance();

24

Tutorial – XML Programming in Java

Section 5 – Advanced parser functions Adding Nodes to our Document Now that we have our Document object, we can start creating Nodes. The first Node we’ll create is a <sonnet> element. We’ll create all the Nodes we need, then add each one to its appropriate parent. Notice that we used the setAttribute method to set the value of the type attribute for the <sonnet> element.

Element root = doc. createElement("sonnet"); root.setAttribute("type", "Shakespearean");

Establishing your document structure As we continue to build our DOM tree, we’ll need to create the structure of our document. To do this, we’ll use the appendChild method appropriately. We’ll create the <author> element, then create the various elements that belong beneath it, then use appendChild to add all of those elements to the correct parent. Notice that createElement is a method of the Document class. Our Document object owns all of the elements we create here. Finally, notice that we create Text nodes for the content of all elements. The Text node is the child of the element, and the Text node’s parent is then added to the appropriate parent.

Element author = doc.createElement("author"); Element lastName = doc. createElement("last-name"); lastName.appendChild(doc. createTextNode("Shakespeare")); author.appendChild(lastName);

Finishing our DOM tree
Element line14 = doc. createElement("line"); line14.appendChild(doc. createTextNode("As any she ...")); text.appendChild(line14); root.appendChild(text); doc.appendChild(root); domBuilder db = new domBuilder(); db.printDOMTree(doc);

Once we’ve added everything to our <sonnet> element, we need to add it to the Document object. We call the appendChild method one last time, this time appending the child element to the Document object itself. Remember that an XML document can have only one root element; appendChild will throw an exception if you try to add more than one root element to the Document. When we have the DOM tree built, we create a domBuilder object, then call its printDOMTree method to print the DOM tree.

25

Section 5 – Advanced parser functions

Tutorial – XML Programming in Java Using DOM objects to avoid parsing You can think of a DOM Document object as the compiled form of an XML document. If you’re using XML to move data from one place to another, you’ll save a lot of time and effort if you can send and receive DOM objects instead of XML source. This is one of the most common reasons why you might want to build a DOM tree from scratch. In the worst case, you would have to create XML source from a DOM tree before you sent your data out, then you’d have to create a DOM tree when you received the XML data. Using DOM objects directly saves a great deal of time. One caveat: be aware that a DOM object may be significantly larger than the XML source. If you have to send your data across a slow connection, sending the smaller XML source might more than make up for the wasted processing time spent reparsing your data.

Parsing an XML string There may be times when you need to parse an XML string. IBM’s XML4J parser supports this, although you have to convert your string into an InputSource object. parseString ps = new parseString(); StringReader sr = new StringReader("<?xml version=\"1.0\"?> <a>Alpha<b>Bravo</b> <c>Charlie</c></a>"); InputSource iSrc = new InputSource(sr); ps.parseAndPrint(iSrc);

The first step is to create a StringReader object from your string. Once you’ve done that, you can create an InputSource from the StringReader. You can run java parseString to see this code in action. In this sample application, the XML string is hardcoded; there are any number of ways you could get XML input from a user or another machine. With this technique, you don’t have to write the XML document to a file system to parse it. The parseString.java source code is on page 48.

26

Tutorial – XML Programming in Java

Section 5 – Advanced parser functions Sorting Nodes in a DOM tree To demonstrate how you can change the structure of a DOM tree, we’ll change our DOM sample to sort the <line>s of the sonnet. There are several DOM methods that make it easy to move Nodes around the DOM tree. To see this code in action, run java domSorter sonnet.xml. It doesn’t do much for the rhyme scheme, but it does correctly sort the <line> elements. To begin the task of sorting, we’ll use the getElementsByTagName method to retrieve all of the <line> elements in the document. This method saves us the trouble of writing code to traverse the entire tree. The domSorter.java source code is on page 50.

if (doc != null) { sortLines(doc); printDOMTree(doc); } ... public void sortLines(Document doc) { NodeList theLines = doc.getDocumentElement(). getElementsByTagName("line");

...

public String getTextFromLine(Node lineElement) { StringBuffer returnString = new StringBuffer(); if (lineElement.getNodeName(). equals("line")) { NodeList kids = lineElement. getChildNodes(); if (kids != null) if (kids.item(0).getNodeType() == Node.TEXT_NODE) returnString.append(kids.item(0). getNodeValue()); } else returnString.setLength(0); return new String(returnString); }

Retrieving the text of our <line>s To simplify the code, we created a helper function, getTextFromLine, that retrieves the text contained inside a <line> element. It simply looks at the <line> element’s first child, and returns its text if that first child is a Text node. This method returns a Java String so that our sort routine can use the String.compareTo method to determine the sorting order. This code actually should check all of the <line>’s children, because it could contain entity references (say the entity &miss; was defined for the text “mistress”). We’ll leave this improvement as an exercise for the reader.

27

Section 5 – Advanced parser functions

Tutorial – XML Programming in Java Sorting the text Now that we have the ability to get the text from a given <line> element, we’re ready to sort the data. Because we only have 14 elements, we’ll use a bubble sort. The bubble sort algorithm compares two adjacent values, and swaps them if they’re out of order. To do the swap, we use the getParentNode and insertBefore methods. getParentNode returns the parent of any Node; we use this method to get the parent of the current <line> (a <lines> element for documents using the sonnet DTD). insertBefore(nodeA, nodeB) inserts nodeA into the DOM tree before nodeB. The most important feature of insertBefore is that if nodeA already exists in the DOM tree, it is removed from its current position and inserted before nodeB.

public void sortLines(Document doc) { NodeList theLines = doc.getDocumentElement(). getElementsByTagName("line"); if (theLines != null) { int len = theLines.getLength(); for (int i=0; i < len; i++) for (int j=0; j < (len-1-i); j++) if (getTextFromLine( theLines.item(j)). compareTo(getTextFromLine( theLines.item(j+1))) > 0) theLines.item(j). getParentNode().insertBefore( theLines.item(j+1), theLines.item(j)); } }

Useful DOM methods for tree manipulation In addition to insertBefore, there are several other DOM methods that are useful for tree manipulations. parentNode.appendChild(newChild); ... parentNode.insertBefore(newChild); ... parentNode.replaceChild(newChild, oldChild); ... parentNode.removeChild(oldChild) ...







parentNode.appendChild(newChild) Appends a node as the last child of a given parent node. Calling parentNode.insertBefore(newChild, null) does the same thing. parentNode.replaceChild(newChild, oldChild) Replaces oldChild with newChild. The node oldChild must be a child of parentNode. parentNode.removeChild(oldChild) Removes oldChild from parentNode.

28

Tutorial – XML Programming in Java

Section 5 – Advanced parser functions One more thing about tree manipulation If you need to remove all the children of a given node, be aware that it’s more difficult than it seems. Both code samples at the left look like they would work. However, only the one on the bottom actually works. The first sample doesn’t work because kid’s instance data is updated as soon as removeChild(kid) is called. In other words, the for loop removes kid, the first child, then checks to see if kid.getNextSibling is null. Because kid has just been removed, it no longer has any siblings, so kid.getNextSibling is null. The for loop will never run more than once. Whether node has one child or a thousand, the first code sample only removes the first child. Be sure to use the second code sample to remove all child nodes.

/** Doesn’t work **/ for (Node kid = node.getFirstChild(); kid != null; kid = kid.getNextSibling()) node.removeChild(kid);

/** Does work **/ while (node.hasChildNodes()) node.removeChild(node.getFirstChild());

Using a different DOM parser Although we can’t think of a single reason why you’d want to, you can use a parser other than XML4J to parse your XML document. If you look at domTwo.java, you’ll see that changing to Sun’s XML parser required only two changes. First of all, we had to import the files for Sun’s classes. That’s simple enough. The only other thing we had to change was the code that creates the Parser object. As you can see, setup for Sun’s parser is a little more complicated, but the rest of the code is unchanged. All of the DOM code works without any changes. Finally, the only other difference in domTwo is the command line format. For some reason, Sun’s parser doesn’t resolve file names in the same way. If you run java domTwo file:///d:/sonnet.xml (modifying the file URI based on your system, of course), you’ll see the same results you saw with domOne. The domTwo.java source code is on page 54.

import com.sun.xml.parser.Parser; import com.sun.xml.tree.XmlDocumentBuilder; ... XmlDocumentBuilder builder = new XmlDocumentBuilder(); Parser parser = new com.sun.xml.parser.Parser(); parser.setDocumentHandler(builder); builder.setParser(parser); parser.parse(uri); doc = builder.getDocument();

29

Section 5 – Advanced parser functions import com.sun.xml.parser.Resolver; ... try { Parser parser = ParserFactory.makeParser(); parser.setDocumentHandler(this); parser.setErrorHandler(this); parser.parse(Resolver. createInputSource(new File(uri))); }

Tutorial – XML Programming in Java Using a different SAX parser We also created saxTwo.java to illustrate using Sun’s SAX parser. As with domTwo, we made two basic changes. The first was to import Sun’s Resolver class instead of IBM’s SAXParser class. We had to change the line that creates the Parser object, and we had to create an InputSource object based on the URI we entered. The only other change we had to make is that the line that creates the parser has to be inside a try block in case we get an exception when we create the Parser object. The saxTwo.java source code is on page 56.

Summary In this section, we’ve demonstrated some advanced coding techniques you can use with XML parsers. We demonstrated ways to generate DOM trees directly, how to parse strings as opposed to files, how to move items around in a DOM tree, and how changing parsers doesn’t affect code written to the DOM and SAX standards.

Hope you enjoyed the show! That’s about it for this tutorial. We’ve talked about the basic architecture of XML applications, and we’ve shown you how to work with XML documents. Future tutorials will cover more details of building XML applications, including: • • • Using visual tools to build XML applications Transforming an XML document from one vocabulary to another Creating front-end interfaces to end users or other processes, and creating back-end interfaces to data stores

30

Tutorial – XML Programming in Java

Section 5 – Advanced parser functions For more information If you’d like to know more about XML, check out the XML zone of developerWorks. The site has code samples, other tutorials, information about XML standards efforts, and lots more. Finally, we’d love to hear from you! We’ve designed developerWorks to be a resource for developers. If you have any comments, suggestions, or complaints about the site, let us know. Thanks, -Doug Tidwell

31

Appendix – Listings of our samples

Tutorial – XML Programming in Java

Appendix – Listings of our samples
This section lists all of the samples discussed in the tutorial. The listings include the Java source and the XML documents used as samples.

sonnet.xml
This is the sample XML document used throughout the tutorial. <?xml version="1.0"?> <!DOCTYPE sonnet SYSTEM "sonnet.dtd"> <sonnet type="Shakespearean"> <author> <last-name>Shakespeare</last-name> <first-name>William</first-name> <nationality>British</nationality> <year-of-birth>1564</year-of-birth> <year-of-death>1616</year-of-death> </author> <title>Sonnet 130</title> <text> <line>My mistress' eyes are nothing like the sun,</line> <line>Coral is far more red than her lips red.</line> <line>If snow be white, why then her breasts are dun,</line> <line>If hairs be wires, black wires grow on her head.</line> <line>I have seen roses damasked, red and white,</line> <line>But no such roses see I in her cheeks.</line> <line>And in some perfumes is there more delight</line> <line>Than in the breath that from my mistress reeks.</line> <line>I love to hear her speak, yet well I know</line> <line>That music hath a far more pleasing sound.</line> <line>I grant I never saw a goddess go,</line> <line>My mistress when she walks, treads on the ground.</line> <line>And yet, by Heaven, I think my love as rare</line> <line>As any she belied with false compare.</line> </text> </sonnet>

sonnet.dtd
This is the DTD for our sample document. <!-- sonnet.dtd --> <!ELEMENT sonnet (author,title?,text) > <!ATTLIST sonnet type (Shakespearean | Petrarchan) <!ELEMENT text (line,line,line,line, line,line,line,line, line,line,line,line, line,line) > <!ELEMENT author (last-name,first-name,nationality, year-of-birth?,year-of-death?) >

"Shakespearean">

<!ELEMENT title (#PCDATA)> 32

Tutorial – XML Programming in Java <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT last-name (#PCDATA)> first-name (#PCDATA)> nationality (#PCDATA)> year-of-birth (#PCDATA)> year-of-death (#PCDATA)> line (#PCDATA)>

Appendix – Listings of our samples

domOne.java
This is our first DOM application. It parses an XML document and writes its contents to standard output.
/* * (C) Copyright IBM Corp. 1999 All rights reserved. * * US Government Users Restricted Rights Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import import import import import import org.w3c.dom.Attr; org.w3c.dom.Document; org.w3c.dom.NamedNodeMap; org.w3c.dom.Node; org.w3c.dom.NodeList; com.ibm.xml.parsers.*;

/** * domOne.java * Illustrates how to go through a DOM tree. */ public class domOne { public void parseAndPrint(String uri) { Document doc = null; try { DOMParser parser = new DOMParser(); parser.parse(uri); doc = parser.getDocument(); } catch (Exception e) { System.err.println("Sorry, an error occurred: " + e); } // We've parsed the document now, so let's print it.

33

Appendix – Listings of our samples if (doc != null) printDOMTree(doc); }

Tutorial – XML Programming in Java

/** Prints the specified node, then prints all of its children. */ public void printDOMTree(Node node) { int type = node.getNodeType(); switch (type) { // print the document element case Node.DOCUMENT_NODE: { System.out.println("<?xml version=\"1.0\" ?>"); printDOMTree(((Document)node).getDocumentElement()); break; } // print element with attributes case Node.ELEMENT_NODE: { System.out.print("<"); System.out.print(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); System.out.print(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\""); } System.out.println(">"); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) printDOMTree(children.item(i)); } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { System.out.print("&"); System.out.print(node.getNodeName()); System.out.print(";"); break; } // print cdata sections case Node.CDATA_SECTION_NODE: { System.out.print("<![CDATA["); System.out.print(node.getNodeValue()); System.out.print("]]>"); break; } // print text

34

Tutorial – XML Programming in Java case Node.TEXT_NODE: { System.out.print(node.getNodeValue()); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { System.out.print("<?"); System.out.print(node.getNodeName()); String data = node.getNodeValue(); { System.out.print(" "); System.out.print(data); } System.out.print("?>"); break; } } if (type == Node.ELEMENT_NODE) { System.out.println(); System.out.print("</"); System.out.print(node.getNodeName()); System.out.print('>'); } }

Appendix – Listings of our samples

/** Main program entry point. */ public static void main(String argv[]) { if (argv.length == 0) { System.out.println("Usage: java domOne uri"); System.out.println(" where uri is the URI of the XML document you want to print."); Sample: java domOne sonnet.xml"); System.out.println(" System.exit(1); } domOne d1 = new domOne(); d1.parseAndPrint(argv[0]); }

}

domCounter.java
This code parses an XML document, then goes through the DOM tree to gather statistics about the document. When the statistics are calculated, the code writes them to standard output.
/* * * * * * * * * * (C) Copyright IBM Corp. 1999 All rights reserved.

US Government Users Restricted Rights Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. The program is provided "as is" without any warranty express or implied, including the warranty of non-infringement and the implied warranties of merchantibility and fitness for a particular purpose. IBM will not be liable for any damages suffered by you as a result

35

Appendix – Listings of our samples

Tutorial – XML Programming in Java

* of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import import import import org.w3c.dom.Document; org.w3c.dom.Node; org.w3c.dom.NodeList; com.ibm.xml.parsers.DOMParser;

/** * domCounter.java * This code creates a DOM parser, parses a document, then * prints statistics about the number and type of nodes * found in the document. */ public class domCounter { int documentNodes = 0; int elementNodes = 0; int entityReferenceNodes = 0; int cdataSections = 0; int textNodes = 0; int processingInstructions = 0; public void parseAndCount(String uri) { Document doc = null; try { DOMParser parser = new DOMParser(); parser.parse(uri); doc = parser.getDocument(); } catch (Exception e) { System.err.println("Sorry, an error occurred: " + e); } // We've parsed the document now, so let's scan the DOM tree and // print the statistics. if (doc != null) { scanDOMTree(doc); System.out.println("Document Statistics for " + uri + ":"); System.out.println("===================================="); System.out.println("Document Nodes: " + documentNodes); System.out.println("Element Nodes: " + elementNodes); System.out.println("Entity Reference Nodes: " + entityReferenceNodes); System.out.println("CDATA Sections: " + cdataSections); System.out.println("Text Nodes: " + textNodes); System.out.println("Processing Instructions: " + processingInstructions); System.out.println(" ----------"); int totalNodes = documentNodes + elementNodes + entityReferenceNodes + cdataSections + textNodes + processingInstructions;

36

Tutorial – XML Programming in Java
System.out.println("Total: } }

Appendix – Listings of our samples
" + totalNodes + " Nodes");

/** Scans the DOM tree and counts the different types of nodes. */ public void scanDOMTree(Node node) { int type = node.getNodeType(); switch (type) { case Node.DOCUMENT_NODE: documentNodes++; scanDOMTree(((Document)node).getDocumentElement()); break; case Node.ELEMENT_NODE: elementNodes++; NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) scanDOMTree(children.item(i)); } break; case Node.ENTITY_REFERENCE_NODE: entityReferenceNodes++; break; case Node.CDATA_SECTION_NODE: cdataSections++; break; case Node.TEXT_NODE: textNodes++; break; case Node.PROCESSING_INSTRUCTION_NODE: processingInstructions++; break; } } /** Main program entry point. */ public static void main(String argv[]) { if (argv.length == 0) { System.out.println("Usage: java domCounter uri"); System.out.println(" where uri is the URI of your XML document."); System.out.println(" Sample: java domCounter sonnet.xml"); System.exit(1); } domCounter dc = new domCounter(); dc.parseAndCount(argv[0]); } }

saxOne.java
This is our first SAX application. It parses an XML document and writes its contents to standard output. 37

Appendix – Listings of our samples

Tutorial – XML Programming in Java

/* * (C) Copyright IBM Corp. 1999 All rights reserved. * * US Government Users Restricted Rights Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import import import import import import org.xml.sax.AttributeList; org.xml.sax.HandlerBase; org.xml.sax.Parser; org.xml.sax.SAXException; org.xml.sax.SAXParseException; org.xml.sax.helpers.ParserFactory;

import com.ibm.xml.parsers.SAXParser; /** * saxOne.java * This sample program illustrates how to use a SAX parser. It * parses a document and writes the document’s contents back to * standard output. */ public class saxOne extends HandlerBase { public void parseURI(String uri) { SAXParser parser = new SAXParser(); parser.setDocumentHandler(this); parser.setErrorHandler(this); try { parser.parse(uri); } catch (Exception e) { System.err.println(e); } } /** Processing instruction. */ public void processingInstruction(String target, String data) { System.out.print("<?"); System.out.print(target); if (data != null && data.length() > 0) { System.out.print(' ');

38

Tutorial – XML Programming in Java
System.out.print(data); } System.out.print("?>"); } /** Start document. */ public void startDocument() { System.out.println("<?xml version=\"1.0\"?>"); }

Appendix – Listings of our samples

/** Start element. */ public void startElement(String name, AttributeList attrs) { System.out.print("<"); System.out.print(name); if (attrs != null) { int len = attrs.getLength(); for (int i = 0; i < len; i++) { System.out.print(" "); System.out.print(attrs.getName(i)); System.out.print("=\""); System.out.print(attrs.getValue(i)); System.out.print("\""); } } System.out.print(">"); } /** Characters. */ public void characters(char ch[], int start, int length) { System.out.print(new String(ch, start, length)); } /** Ignorable whitespace. */ public void ignorableWhitespace(char ch[], int start, int length) { characters(ch, start, length); } /** End element. */ public void endElement(String name) { System.out.print("</"); System.out.print(name); System.out.print(">"); } /** End document. */ public void endDocument() { // No need to do anything. } // // ErrorHandler methods // /** Warning. */

39

Appendix – Listings of our samples

Tutorial – XML Programming in Java

public void warning(SAXParseException ex) { System.err.println("[Warning] "+ getLocationString(ex)+": "+ ex.getMessage()); } /** Error. */ public void error(SAXParseException ex) { System.err.println("[Error] "+ getLocationString(ex)+": "+ ex.getMessage()); } /** Fatal error. */ public void fatalError(SAXParseException ex) throws SAXException { System.err.println("[Fatal Error] "+ getLocationString(ex)+": "+ ex.getMessage()); throw ex; } /** Returns a string of the location. */ private String getLocationString(SAXParseException ex) { StringBuffer str = new StringBuffer(); String systemId = ex.getSystemId(); if (systemId != null) { int index = systemId.lastIndexOf('/'); if (index != -1) systemId = systemId.substring(index + 1); str.append(systemId); } str.append(':'); str.append(ex.getLineNumber()); str.append(':'); str.append(ex.getColumnNumber()); return str.toString(); } /** Main program entry point. */ public static void main(String argv[]) { if (argv.length == 0) { System.out.println("Usage: java saxOne uri"); System.out.println(" where uri is the URI of your XML document."); System.out.println(" Sample: java saxOne sonnet.xml"); System.exit(1); } saxOne s1 = new saxOne(); s1.parseURI(argv[0]); } }

40

Tutorial – XML Programming in Java

Appendix – Listings of our samples

saxCounter.java
This code parses an XML document and calculates statistics about the document as it receives SAX events. When the entire document has been parsed, the code writes the statistics to standard output.
/* * (C) Copyright IBM Corp. 1999 All rights reserved. * * US Government Users Restricted Rights Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import import import import import import org.xml.sax.AttributeList; org.xml.sax.HandlerBase; org.xml.sax.Parser; org.xml.sax.SAXException; org.xml.sax.SAXParseException; org.xml.sax.helpers.ParserFactory;

import com.ibm.xml.parsers.SAXParser; /** * saxCounter.java * This sample program calculates statistics for an XML document, * based on the SAX events received. When the parse is complete, * it prints the statistics to standard output. */ public class saxCounter extends HandlerBase { int int int int int int int int int int startDocumentEvents = 0; endDocumentEvents = 0; startElementEvents = 0; endElementEvents = 0; processingInstructionEvents = 0; characterEvents = 0; ignorableWhitespaceEvents = 0; warningEvents = 0; errorEvents = 0; fatalErrorEvents = 0;

public void parseURI(String uri) { SAXParser parser = new SAXParser(); parser.setDocumentHandler(this); parser.setErrorHandler(this); try {

41

Appendix – Listings of our samples parser.parse(uri); } catch (Exception e) { System.err.println(e); }

Tutorial – XML Programming in Java

System.out.println("Document Statistics for " + uri + ":"); System.out.println("===================================="); System.out.println("DocumentHandler Events:"); System.out.println(" startDocument " + startDocumentEvents); System.out.println(" endDocument " + endDocumentEvents); System.out.println(" startElement " + startElementEvents); System.out.println(" endElement " + endElementEvents); System.out.println(" processingInstruction " + processingInstructionEvents); System.out.println(" character " + characterEvents); System.out.println(" ignorableWhitespace " + ignorableWhitespaceEvents); System.out.println("ErrorHandler Events:"); System.out.println(" warning " + warningEvents); System.out.println(" error " + errorEvents); System.out.println(" fatalError " + fatalErrorEvents); System.out.println(" ----------"); int totalEvents = startDocumentEvents + endDocumentEvents + startElementEvents + endElementEvents + processingInstructionEvents + characterEvents + ignorableWhitespaceEvents + warningEvents + errorEvents + fatalErrorEvents; System.out.println("Total: " + totalEvents + " Events"); } /** Processing instruction. */ public void processingInstruction(String target, String data) { processingInstructionEvents++; } /** Start document. */ public void startDocument() { startDocumentEvents++; } /** Start element. */ public void startElement(String name, AttributeList attrs) { startElementEvents++; } /** Characters. */ public void characters(char ch[], int start, int length) { characterEvents++;

42

Tutorial – XML Programming in Java
}

Appendix – Listings of our samples

/** Ignorable whitespace. */ public void ignorableWhitespace(char ch[], int start, int length) { ignorableWhitespaceEvents++; } /** End element. */ public void endElement(String name) { endElementEvents++; } /** End document. */ public void endDocument() { endDocumentEvents++; } // // ErrorHandler methods // /** Warning. */ public void warning(SAXParseException ex) { warningEvents++; } /** Error. */ public void error(SAXParseException ex) { errorEvents++; } /** Fatal error. */ public void fatalError(SAXParseException ex) throws SAXException { fatalErrorEvents++; throw ex; } /** Main program entry point. */ public static void main(String argv[]) { if (argv.length == 0) { System.out.println("Usage: java saxCounter uri"); System.out.println(" where uri is the URI of your XML document."); System.out.println(" Sample: java saxCounter sonnet.xml"); System.exit(1); } saxCounter sc = new saxCounter(); sc.parseURI(argv[0]); } }

43

Appendix – Listings of our samples

Tutorial – XML Programming in Java

domBuilder.java
This code builds a DOM tree without using an XML document as source. When the tree is complete, this code writes the tree’s contents to standard output.
/* * (C) Copyright IBM Corp. 1999 All rights reserved. * * US Government Users Restricted Rights Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import import import import import import import org.w3c.dom.Attr; org.w3c.dom.Document; org.w3c.dom.Element; org.w3c.dom.NamedNodeMap; org.w3c.dom.Node; org.w3c.dom.NodeList; com.ibm.xml.parsers.*;

/** * domBuilder.java * This sample program illustrates how to create a DOM tree from scratch. */ public class domBuilder { /** Prints the specified node, recursively. */ public void printDOMTree(Node node) { int type = node.getNodeType(); switch (type) { // print the document element case Node.DOCUMENT_NODE: { System.out.println("<?xml version=\"1.0\" ?>"); printDOMTree(((Document)node).getDocumentElement()); break; } // print element with attributes case Node.ELEMENT_NODE: { System.out.print("<"); System.out.print(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i);

44

Tutorial – XML Programming in Java
System.out.print(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\""); } System.out.println(">"); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) printDOMTree(children.item(i)); } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { System.out.print("&"); System.out.print(node.getNodeName()); System.out.print(";"); break; } // print cdata sections case Node.CDATA_SECTION_NODE: { System.out.print("<![CDATA["); System.out.print(node.getNodeValue()); System.out.print("]]>"); break; } // print text case Node.TEXT_NODE: { System.out.print(node.getNodeValue()); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { System.out.print("<?"); System.out.print(node.getNodeName()); String data = node.getNodeValue(); { System.out.print(" "); System.out.print(data); } System.out.print("?>"); break; } } if (type == Node.ELEMENT_NODE) { System.out.println(); System.out.print("</"); System.out.print(node.getNodeName()); System.out.print('>');

Appendix – Listings of our samples

45

Appendix – Listings of our samples
} }

Tutorial – XML Programming in Java

/** Main program entry point. */ public static void main(String argv[]) { if (argv.length == 1 && argv[0].equals("-help")) { System.out.println("Usage: java domBuilder"); System.out.println(" This code builds a DOM tree, then prints it."); System.exit(1); } try { Document doc = (Document)Class. forName("com.ibm.xml.dom.DocumentImpl"). newInstance(); Element root = doc.createElement("sonnet"); root.setAttribute("type", "Shakespearean"); Element author = doc.createElement("author"); Element lastName = doc.createElement("last-name"); lastName.appendChild(doc.createTextNode("Shakespeare")); author.appendChild(lastName); Element firstName = doc.createElement("first-name"); firstName.appendChild(doc.createTextNode("William")); author.appendChild(firstName); Element nationality = doc.createElement("nationality"); nationality.appendChild(doc.createTextNode("British")); author.appendChild(nationality); Element yearOfBirth = doc.createElement("year-of-birth"); yearOfBirth.appendChild(doc.createTextNode("1564")); author.appendChild(yearOfBirth); Element yearOfDeath = doc.createElement("year-of-death"); yearOfDeath.appendChild(doc.createTextNode("1616")); author.appendChild(yearOfDeath); root.appendChild(author); Element title = doc.createElement("title"); title.appendChild(doc.createTextNode("Sonnet 130")); root.appendChild(title); Element text = doc.createElement("text"); Element line01 = doc.createElement("line"); line01.appendChild(doc.createTextNode("My mistress' eyes are nothing like the sun,")); text.appendChild(line01); Element line02 = doc.createElement("line"); line02.appendChild(doc.createTextNode("Coral is far more red than her lips red.")); text.appendChild(line02); Element line03 = doc.createElement("line");

46

Tutorial – XML Programming in Java

Appendix – Listings of our samples

line03.appendChild(doc.createTextNode("If snow be white, why then her breasts are dun,")); text.appendChild(line03); Element line04 = doc.createElement("line"); line04.appendChild(doc.createTextNode("If hairs be wires, black wires grow on her head.")); text.appendChild(line04); Element line05 = doc.createElement("line"); line05.appendChild(doc.createTextNode("I have seen roses damasked, red and white,")); text.appendChild(line05); Element line06 = doc.createElement("line"); line06.appendChild(doc.createTextNode("But no such roses see I in her cheeks.")); text.appendChild(line06); Element line07 = doc.createElement("line"); line07.appendChild(doc.createTextNode("And in some perfumes is there more delight")); text.appendChild(line07); Element line08 = doc.createElement("line"); line08.appendChild(doc.createTextNode("Than in the breath that from my mistress reeks.")); text.appendChild(line08); Element line09 = doc.createElement("line"); line09.appendChild(doc.createTextNode("I love to hear her speak, yet well I know")); text.appendChild(line09); Element line10 = doc.createElement("line"); line10.appendChild(doc.createTextNode("That music hath a far more pleasing sound.")); text.appendChild(line10); Element line11 = doc.createElement("line"); line11.appendChild(doc.createTextNode("I grant I never saw a goddess go,")); text.appendChild(line11); Element line12 = doc.createElement("line"); line12.appendChild(doc.createTextNode("My mistress when she walks, treads on the ground.")); text.appendChild(line12); Element line13 = doc.createElement("line"); line13.appendChild(doc.createTextNode("And yet, by Heaven, I think my love as rare")); text.appendChild(line13); Element line14 = doc.createElement("line"); line14.appendChild(doc.createTextNode("As any she belied with false compare.")); text.appendChild(line14); root.appendChild(text); doc.appendChild(root); domBuilder db = new domBuilder(); db.printDOMTree(doc);

47

Appendix – Listings of our samples
} catch (Exception e) { System.err.println(e); } }

Tutorial – XML Programming in Java

}

parseString.java
This code illustrates how to parse a string that contains an XML document.
/* * (C) Copyright IBM Corp. 1999 All rights reserved. * * US Government Users Restricted Rights Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import import import import import import import import import import import import java.io.OutputStreamWriter; java.io.PrintWriter; java.io.UnsupportedEncodingException; java.io.Reader; java.io.StringReader; org.w3c.dom.Attr; org.w3c.dom.Document; org.w3c.dom.NamedNodeMap; org.w3c.dom.Node; org.w3c.dom.NodeList; org.xml.sax.InputSource; com.ibm.xml.parsers.*;

/** * parseString.java * This sample program illustrates how to parse an XML document * contained in a String. */ public class parseString { public void parseAndPrint(InputSource xmlSource) { Document doc = null; try { DOMParser parser = new DOMParser(); parser.parse(xmlSource); doc = parser.getDocument(); } catch (Exception e) { System.err.println("Sorry, an error occurred: " + e);

48

Tutorial – XML Programming in Java
} // We've parsed the document now, so let's print it. if (doc != null) printDOMTree(doc); }

Appendix – Listings of our samples

/** Prints the specified node, recursively. */ public void printDOMTree(Node node) { int type = node.getNodeType(); switch (type) { // print the document element case Node.DOCUMENT_NODE: { System.out.println("<?xml version=\"1.0\" ?>"); printDOMTree(((Document)node).getDocumentElement()); break; } // print element with attributes case Node.ELEMENT_NODE: { System.out.print("<"); System.out.print(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); System.out.print(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\""); } System.out.println(">"); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) printDOMTree(children.item(i)); } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { System.out.print("&"); System.out.print(node.getNodeName()); System.out.print(";"); break; } // print cdata sections case Node.CDATA_SECTION_NODE: { System.out.print("<![CDATA["); System.out.print(node.getNodeValue()); System.out.print("]]>");

49

Appendix – Listings of our samples break; } // print text case Node.TEXT_NODE: { System.out.print(node.getNodeValue()); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { System.out.print("<?"); System.out.print(node.getNodeName()); String data = node.getNodeValue(); { System.out.print(" "); System.out.print(data); } System.out.print("?>"); break; } } if (type == Node.ELEMENT_NODE) { System.out.println(); System.out.print("</"); System.out.print(node.getNodeName()); System.out.print('>'); } }

Tutorial – XML Programming in Java

/** Main program entry point. */ public static void main(String argv[]) { parseString ps = new parseString(); StringReader sr = new StringReader("<?xml version=\"1.0\"?><a>Alpha<b>Bravo</b><c>Charlie</c></a>"); InputSource iSrc = new InputSource(sr); ps.parseAndPrint(iSrc); }

}

domSorter.java
This code looks for all the <line> elements in the XML document, then sorts them. It illustrates how to manipulate a DOM tree.
/* * * * * * * * * * * * (C) Copyright IBM Corp. 1999 All rights reserved.

US Government Users Restricted Rights Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. The program is provided "as is" without any warranty express or implied, including the warranty of non-infringement and the implied warranties of merchantibility and fitness for a particular purpose. IBM will not be liable for any damages suffered by you as a result of using the Program. In no event will IBM be liable for any special, indirect or consequential damages or lost profits even if

50

Tutorial – XML Programming in Java

Appendix – Listings of our samples

* IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import import import import import import import org.w3c.dom.Attr; org.w3c.dom.Document; org.w3c.dom.Element; org.w3c.dom.NamedNodeMap; org.w3c.dom.Node; org.w3c.dom.NodeList; com.ibm.xml.parsers.*;

/** * domSorter.java * This sample program illustrates how to rearrange the nodes in a * DOM tree. */ public class domSorter { public void parseAndSortLines(String uri) { Document doc = null; try { DOMParser parser = new DOMParser(); parser.parse(uri); doc = parser.getDocument(); } catch (Exception e) { System.err.println("Sorry, an error occurred: " + e); } // We've parsed the document now, so let's sort it and print it. if (doc != null) { sortLines(doc); printDOMTree(doc); } } public String getTextFromLine(Node lineElement) { StringBuffer returnString = new StringBuffer(); if (lineElement.getNodeName().equals("line")) { NodeList kids = lineElement.getChildNodes(); if (kids != null) { if (kids.item(0).getNodeType() == Node.TEXT_NODE) { returnString.append(kids.item(0).getNodeValue()); } } }

51

Appendix – Listings of our samples else returnString.setLength(0); return new String(returnString); }

Tutorial – XML Programming in Java

/** Sorts the <line> elements in the file. It uses a bubble sort algorithm, since a sonnet only has 14 lines. **/ public void sortLines(Document doc) { NodeList theLines = doc.getDocumentElement(). getElementsByTagName("line"); if (theLines != null) { int len = theLines.getLength(); for (int i = 0; i < len; i++) for (int j = 0; j < (len - 1 - i); j++) if (getTextFromLine(theLines.item(j)). compareTo(getTextFromLine(theLines.item(j+1))) > 0) theLines.item(j).getParentNode(). insertBefore(theLines.item(j+1), theLines.item(j)); } } /** Prints the specified node, recursively. */ public void printDOMTree(Node node) { int type = node.getNodeType(); switch (type) { // print the document element case Node.DOCUMENT_NODE: { System.out.println("<?xml version=\"1.0\" ?>"); printDOMTree(((Document)node).getDocumentElement()); break; } // print element with attributes case Node.ELEMENT_NODE: { System.out.print("<"); System.out.print(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); System.out.print(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\""); } System.out.println(">"); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) printDOMTree(children.item(i)); } break;

52

Tutorial – XML Programming in Java
} // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { System.out.print("&"); System.out.print(node.getNodeName()); System.out.print(";"); break; } // print cdata sections case Node.CDATA_SECTION_NODE: { System.out.print("<![CDATA["); System.out.print(node.getNodeValue()); System.out.print("]]>"); break; } // print text case Node.TEXT_NODE: { if (node.getNodeValue().trim().length() > 0) System.out.print(node.getNodeValue()); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { System.out.print("<?"); System.out.print(node.getNodeName()); String data = node.getNodeValue(); if (data != null && data.length() > 0) { System.out.print(" "); System.out.print(data); } System.out.print("?>"); break; } } if (type == Node.ELEMENT_NODE) { System.out.println(); System.out.print("</"); System.out.print(node.getNodeName()); System.out.print('>'); } }

Appendix – Listings of our samples

/** Main program entry point. */ public static void main(String argv[]) { if (argv.length == 0) { System.out.println("Usage: java domSorter uri"); System.out.println(" where uri is the URI of the XML document you want to sort."); System.out.println(" Sample: java domSorter sonnet.xml"); System.out.println();

53

Appendix – Listings of our samples
System.out.println(" System.exit(1); } domSorter ds = new domSorter(); ds.parseAndSortLines(argv[0]); } }

Tutorial – XML Programming in Java
Note: Your XML document must use the sonnet DTD.");

domTwo.java
This code is identical to domOne.java, except it uses Sun’s XML parser instead of IBM’s. It illustrates the portability of the DOM interfaces.
/* * (C) Copyright IBM Corp. 1999 All rights reserved. * * US Government Users Restricted Rights Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import import import import import org.w3c.dom.Attr; org.w3c.dom.Document; org.w3c.dom.NamedNodeMap; org.w3c.dom.Node; org.w3c.dom.NodeList;

import com.sun.xml.parser.Parser; import com.sun.xml.tree.XmlDocumentBuilder; /** * domTwo.java * Illustrates how to go through a DOM tree. Identical to domOne, * except it uses Sun’s XML parser instead of IBM’s. */ public class domTwo { public void parseAndPrint(String uri) { Document doc = null; try { XmlDocumentBuilder builder = new XmlDocumentBuilder(); Parser parser = new com.sun.xml.parser.Parser(); parser.setDocumentHandler(builder); builder.setParser(parser); builder.setDisableNamespaces(false);

54

Tutorial – XML Programming in Java parser.parse(uri); doc = builder.getDocument();

Appendix – Listings of our samples

} catch (Exception e) { System.err.println("Sorry, an error occurred: " + e); } // We've parsed the document now, so let's print it. if (doc != null) printDOMTree(doc); } /** Prints the specified node, recursively. */ public void printDOMTree(Node node) { int type = node.getNodeType(); switch (type) { // print the document element case Node.DOCUMENT_NODE: { System.out.println("<?xml version=\"1.0\" ?>"); printDOMTree(((Document)node).getDocumentElement()); break; } // print element with attributes case Node.ELEMENT_NODE: { System.out.print("<"); System.out.print(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); System.out.print(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\""); } System.out.println(">"); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) printDOMTree(children.item(i)); } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { System.out.print("&"); System.out.print(node.getNodeName()); System.out.print(";"); break; }

55

Appendix – Listings of our samples
// print cdata sections case Node.CDATA_SECTION_NODE: { System.out.print("<![CDATA["); System.out.print(node.getNodeValue()); System.out.print("]]>"); break; } // print text case Node.TEXT_NODE: { System.out.print(node.getNodeValue()); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { System.out.print("<?"); System.out.print(node.getNodeName()); String data = node.getNodeValue(); { System.out.print(" "); System.out.print(data); } System.out.print("?>"); break; } } if (type == Node.ELEMENT_NODE) { System.out.println(); System.out.print("</"); System.out.print(node.getNodeName()); System.out.print('>'); } }

Tutorial – XML Programming in Java

/** Main program entry point. */ public static void main(String argv[]) { if (argv.length == 0) { System.out.println("Usage: java domTwo uri"); where uri is the URI of the XML document you want to System.out.println(" print."); System.out.println(" Sample: java domTwo sonnet.xml"); System.exit(1); } domTwo d2 = new domTwo(); d2.parseAndPrint(argv[0]); }

}

saxTwo.java
This code is identical to saxOne.java, except it uses Sun’s XML parser instead of IBM’s. It illustrates the portability of the SAX interfaces.

56

Tutorial – XML Programming in Java

Appendix – Listings of our samples

/* * (C) Copyright IBM Corp. 1999 All rights reserved. * * US Government Users Restricted Rights Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import import import import import import import import import import java.io.File; java.io.OutputStreamWriter; java.io.PrintWriter; java.io.UnsupportedEncodingException; org.xml.sax.AttributeList; org.xml.sax.HandlerBase; org.xml.sax.Parser; org.xml.sax.SAXException; org.xml.sax.SAXParseException; org.xml.sax.helpers.ParserFactory;

import com.sun.xml.parser.Resolver; /** * saxTwo.java * This sample program illustrates how to use a SAX parser. It * parses a document and writes the document’s contents back to * standard output. It is identical to saxOne.java except that * it uses Sun’s XML parser instead of IBM’s. */ public class saxTwo extends HandlerBase { public void parseURI(String uri) { try { Parser parser = ParserFactory.makeParser(); parser.setDocumentHandler(this); parser.setErrorHandler(this); parser.parse(Resolver.createInputSource(new File(uri))); } catch (Exception e) { System.err.println(e); } } /** Processing instruction. */ public void processingInstruction(String target, String data) { System.out.print("<?"); System.out.print(target); if (data != null && data.length() > 0) {

57

Appendix – Listings of our samples
System.out.print(' '); System.out.print(data); } System.out.print("?>"); }

Tutorial – XML Programming in Java

/** Start document. */ public void startDocument() { System.out.println("<?xml version=\"1.0\"?>"); } /** Start element. */ public void startElement(String name, AttributeList attrs) { System.out.print("<"); System.out.print(name); if (attrs != null) { int len = attrs.getLength(); for (int i = 0; i < len; i++) { System.out.print(" "); System.out.print(attrs.getName(i)); System.out.print("=\""); System.out.print(attrs.getValue(i)); System.out.print("\""); } } System.out.print(">"); } /** Characters. */ public void characters(char ch[], int start, int length) { System.out.print(new String(ch, start, length)); } /** Ignorable whitespace. */ public void ignorableWhitespace(char ch[], int start, int length) { characters(ch, start, length); } /** End element. */ public void endElement(String name) { System.out.print("</"); System.out.print(name); System.out.print(">"); } /** End document. */ public void endDocument() { // No need to do anything. } // // ErrorHandler methods //

58

Tutorial – XML Programming in Java
/** Warning. */ public void warning(SAXParseException ex) { System.err.println("[Warning] "+ getLocationString(ex)+": "+ ex.getMessage()); } /** Error. */ public void error(SAXParseException ex) { System.err.println("[Error] "+ getLocationString(ex)+": "+ ex.getMessage()); } /** Fatal error. */ public void fatalError(SAXParseException ex) throws SAXException { System.err.println("[Fatal Error] "+ getLocationString(ex)+": "+ ex.getMessage()); throw ex; } /** Returns a string of the location. */ private String getLocationString(SAXParseException ex) { StringBuffer str = new StringBuffer(); String systemId = ex.getSystemId(); if (systemId != null) { int index = systemId.lastIndexOf('/'); if (index != -1) systemId = systemId.substring(index + 1); str.append(systemId); } str.append(':'); str.append(ex.getLineNumber()); str.append(':'); str.append(ex.getColumnNumber()); return str.toString(); }

Appendix – Listings of our samples

/** Main program entry point. */ public static void main(String argv[]) { if (argv.length == 0) { System.out.println("Usage: java saxTwo uri"); System.out.println(" where uri is the URI of your XML document."); System.out.println(" Sample: java saxTwo sonnet.xml"); System.exit(1); } saxTwo s2 = new saxTwo(); s2.parseURI(argv[0]); }

}

59

Similar Documents

Premium Essay

Ssss

...To be discussed in 1st week – during lecture. Group Assignment: Entrepreneur’s Profile (30%) Choose one entrepreneur in given industry (food, hospitality, logistic, retail, transportation, air line system, medicine, manufacturing, insurance and etc) to conduct an interview and write a report on “Entrepreneur’s Profile”. Instruction:- 1. Form a small group, consist of 5 -6 students. 2. Group leader will participate in lucky draw to choose the industry. 3. Group will propose the suitable entrepreneur in given industry to be endorsed by lecturer. The report structure must be based on the following format: 1. Cover Page a) Name of entrepreneur b) Name of Student c) Matrix number d) Group/section e) Lecturer’s name f) Date of submission 2. Table of contents A. Introduction B. Entrepreneur background C. Company background D. The entrepreneur before starting the venture E. The entrepreneur at the time he/she started the venture F. Conclusion (What can you learn from this entrepreneur?) G. Appendixes (picture of entrepreneur and any materials related in preparing the report. Table of Contents (Details) A. Introduction B. Entrepreneur background - Name, age, gender, ethnicity, origin - How did he or she start the business? - What are the factors which lead to the creation of business? C. Company background - Name of company - What...

Words: 1061 - Pages: 5

Premium Essay

Ssss

...3-1) Days sales outstanding = Account receivables/Average sales per day Account receivable = Average sales per day * Days sales outstanding Account receivable = $20000 * 20 Account receivable = $400,000 3-2) Equity multiplier = 1/Equity ratio Equity ratio = 1/Equity multiplier = ½.5 = 0.4 Debt ratio = 1-Equity ratio = 1-0.4 = 0.6 =60% 3-3) Book value per share = Common equity / Shares outstanding = $6000m / 800m = $7.5 Market/book ratio = Market price per share / Book value per share = $75/$7.5 = 10 3-4) Price per share = Price/cash flow ratio * Cash flow per share = $3 * 8 = $24 P/E ratio = Price per share / Earnings per share = $24 / $1.5 = 16 3-5) Common equity = Total assets / Equity multiplier = $50 / 2 = $25 million ROE = Net income available to common stockholders / Common equity = ($100m * 0.03)/$25m = $3m/$25m = 0.12 = 12% 3-6) ROA = Profit margin * Total assets turnover Total assets turnover = ROA / Profit margin = 10% / 2% = 5 ROE = ROA * Equity multiplier Equity multiplier = ROE / ROA = 15% / 10% = 1.5 3-7) Current ratio = Current assets / Current liabilities Current liabilities = Current assets / Current ratio = $3m / 1.5 = $2 million Quick ratio = (Current assets – Inventories) / Current liabilities Inventories = Current assets – (Current liabilities * Quick ratio) = $3m – ($2m * 1) = $3m - $2m = $1 million 4-1) FVN = PV * (1+I)N = $10000*(1+0.1)5 = $10000 * 1.61051 = $16105.10 4-2) PVN =...

Words: 456 - Pages: 2

Premium Essay

Ssss

...‘Framework for the Preparation and Presentation of Financial Statements’ Summary OVERVIEW The conceptual framework applicable on adoption of Australian equivalents to International Financial Reporting Standards (IFRS) will be: • SAC 1 ‘Definition of the Reporting Entity’; • SAC 2 ‘Objective of General Purpose Financial Reporting’; and • ‘Framework’ - Framework for the Preparation and Presentation of Financial Statements. The existing Australian conceptual framework is provided in: • SAC 1 ‘Definition of the Reporting Entity’; • SAC 2 ‘Objective of General Purpose Financial Reporting’; • SAC 3 ‘Qualitative Characteristics of Financial Information’; and • SAC 4 ‘Definition and Recognition of the Elements of Financial Statements’. SAC 1 and SAC 2 will continue to apply in order to provide guidance for the IFRS application paragraphs. The ‘Framework’ will replace SAC 3 and SAC 4 which will be withdrawn. Although the ‘Framework’ is brief in comparison to SAC 3 and SAC 4, the concepts are similar with the key differences being that the ‘Framework’: ( includes prudence as a qualitative characteristic; and ( identifies two components to both income and expenses. INTRODUCTION The conceptual framework sets out the concepts underlying the preparation and presentation of general-purpose financial reports. The accounting standards, rules and guidance applicable under AASB 101 ‘Presentation of Financial...

Words: 1089 - Pages: 5

Premium Essay

Ssss

...Chapter I Background of the Study Introduction Cases of Dengue caused by insects especially mosquitoes are increasing year by year. Dengue is a disease caused by any 1 of 4 related dengue viruses. The viruses are transmitted to humans by the bite of an infected mosquito. There are 2 types of dengue, with dengue hemorrhagic fever being the more severe, and sometimes fatal, though much less common form of the infection. Dengue is endemic in at least 100 countries such as Asia, the Pacific. Dengue cannot be spread directly from person to person, so you can’t catch it from shaking hands, a sneeze, a doorknob or a toilet seat. Dengue is spread from person to person by the bite of an infected mosquito. The mosquito becomes infected with dengue virus when it bites a person who already has the dengue virus in their blood, typically in the time frame just before the infected person starts to have symptoms of the disease. About one week after biting an infected person, the mosquito can transmit the virus while biting a healthy person. A single infected mosquito can infect multiple people, who in turn can infect multiple mosquitoes — and the cycle continues. A bite from a female carrier mosquito can cause you fever, often as high as 104 - 105 degrees Fahrenheit, fatigue, headache, and swollen nymph nodes and might lead to death if it was not treated immediately. A total of 54,659 dengue cases were reported nationwide from January 1 to August 14, 2010, much higher than the recorded...

Words: 815 - Pages: 4

Premium Essay

Ssss

...Review test (classes no. 2) Note: - The aim of the test is to review the material from the last classes. The test won’t be officially evaluated and there won’t be any consequences of solving it - Try to solve the test without looking at the materials from the classes - This is a multiple choice test (in some questions more than one answer might be correct) 1. The morale hazard is: a. a method of risk manipulation, b. external conditions that affect the size of the peril, c. individual response of the insured person caused by the awareness of the insurance coverage, d. all of the above. 2. Which of the following criteria must be satisfied in insurable event? a. a large group of similar items exposed to the same peril, b. the event is independent of the will of the individual, c. definite and measurable losses, d. nonrandom and ordinary losses. 3. An active risk retention is: a. a conscious decision to retain the risk, b. an unconscious decision to retain the risk, c. can be motivated financially, d. all of the above. 4. An example of risk distribution is: a. a partnership, b. an insurance, c. a smoke detector, d. all of the above. 5. The pure risk is a situation which may result in: a. gain, loss or no change, b. loss or no change, c. gain or loss, d. gain or no change. 6. Mark the main types of a hazard: a. physical hazard, b. moral hazard, c. morale hazard d. investing hazard. 7. The physical hazard is: a. personal traits which make people more inclined to dishonesty, b...

Words: 370 - Pages: 2

Free Essay

Ssss

...DASDSAAAASi vous utilisez un lecteur d'écran, cliquez ici pour désactiver la Googlerecherche instantanée. Recherche Environ 7 720 000 résultats (0,32 secondes) Web Images Maps Vidéos Actualités Applications Plus Montréal, QC Changer le lieu Options de recherche Le Web Pages en français Pays : Canada Pages en langue étrangère traduites Plus d'outils Essayez avec cette orthographe : cell phone is a gadget yahoo Résultats de recherche Go Gadgets Iphone / Cell Phone Repair Las Vegas in ... - Yahoo Local local.yahoo.com/info-101935430-go-g... - États-Unis - Traduire cette page Find Go Gadgets Iphone / Cell Phone Repair Las Vegas in Las Vegas with Address, Phone number from Yahoo! US Local. Includes Go Gadgets Iphone / Cell ... Mobile Phones » Coolest Gadgets www.coolest-gadgets.com/category/mobile-phones/ - Traduire cette page Simply plug the gadget into your home phone system via phone cord. Then connect up to three cell phones via Bluetooth. That's it. When your cell phone rings ... Cell Phone Goodies - ThinkGeek :: Stuff for Smart Masses www.thinkgeek.com › Gadgets - Traduire cette page Items 1 - 70 – Selling geek t-shirts, mugs, ties, high caffeine products, and many other gifts for programmers, linux hackers, and open source geeks. What is a widget wiki.answers.com › ... › Name Origins - Traduire cette page Widgets, go to the official website, http://widgets.yahoo.com/ then click on a button ... In Cell phone lingo a widget is a shortcut that...

Words: 510 - Pages: 3

Free Essay

Ssss

...DMC COLLEGE FOUNDATION INC School of Hotel, Restaurant and Institution Management Dipolog City PRACTICUM EVALUATION Name of Student: ____________________________________________ Date: ___________________ Direction: Please rate the student using the following scoring system: 5 --- EXCELLENT 4 --- VERY GOOD 3 --- GOOD 2 --- NEEDS IMPROVEMENT 1 --- UNSATISFACTORY NA --- NOT APPLICABLE CRITERIA | RATING | I. JOB PERFORMANCE | | 1. Does he/she have the know-how and is well informed as the nature and details of the duties and responsibilities assigned to him/her? | | 2. Does he/she consider the amount of work accomplished and the promptness with which the work completed? | | 3. Is he/she able to perform his/her work with accuracy and neatness? | | 4. Does he/she turn in work that is up to standards? | | 5. Does he/she show honesty and trustworthiness in the observance of confidential matters and the proper use of official time? | | 6. Is he/she careful in handling materials and equipment entrusted to him/her? | | 7. Does he/she practice preservation, conservation and recycling of materials entrusted to him/her? | | II. PERSONAL QUALITIES | | 1. Does he/she have good appearance: wear proper attire for work; is neat and well-groomed? | | 2. Does he/she have strong sense of personal initiative? Is he/she able to think and plan by himself/herself...

Words: 338 - Pages: 2

Premium Essay

Ssss

...Smile Consumer Behaviour - chapter 1 - January 2nd, 2009 hie .. these are consumer behaviour schiffman notes .. il b uploading chapter wise (chpter 1 - Chpter 12)... unable to attach them .. CHAPTER 1 Consumer Behavior: Its Origins and Strategic Applications LEARNING OBJECTIVES After studying this chapter students should be able to: 1. Understand the impact of the digital revolution on general consumer behavior. 2. Define consumer behavior. 3. Identify the two major approaches to the study of consumer behavior. 4. Understand the development of the marketing concept. 5. Understand the role of consumer research in the study of consumer behavior. 6. Understand how segmentation, targeting, and positioning are used in the study of consumer behavior. 7. Define customer value, satisfaction, and retention. 8. Discuss the role of ethics in marketing. 9. Describe the societal marketing concept. 10. Briefly discuss the three interlocking stages of consumer decision-making. CHAPTER SUMMARY The study of consumer behavior enables marketers to understand and predict consumer behavior in the marketplace; it is concerned not only with what consumers buy but also with why, when, where, and how they buy it. Consumer research is the methodology used to study consumer behavior; it takes place at every phase of the consumption process: before the purchase, during the purchase, and after the purchase. Consumer behavior is interdisciplinary; that is, it is based on...

Words: 3261 - Pages: 14

Premium Essay

Ssss

...Copyright 2013 Wesley Methodist Church Klang. Songs Library Powered by WebTeam @ Klang Wesley Above All Writer/Composer : Paul Baloche Original Key : A | Current Key : A | | | Print Version : Small | Medium | Large | Intro A D Esus E A D/A Verse A E/D D Esus E AAbove all powers, a - bove all kingsA/C# E/D D Esus E AAbove all na - ture and all created thingsA/G# F#m Amaj7/E D A/C#Above all wisdom and all the ways of manBm7 D/A E/G# AYou were here before the world began A/C# E/D D Esus E AAbove all kingdoms, above all thrones A/C# E/D D Esus E AAbove all wonders the world has ever known A/G# F#m Amaj7/E D A/C#Above all wealth and treasures of the earthBm7 D/A C#sus/G# C#There’s no way to measure what You’re worth Chorus A Bm7 E/G# ACrucified, laid behind the stone Bm7 E/G# AYou lived to die, rejected and alone A/G# F#m Amaj7/E D A/C#Like a rose trampled on the ground Bm7 A/C# D2 EsusYou took the fall and thought of meE A D Esus E A D/A AAbove all Above All Writer/Composer : Paul Baloche Original Key : A | Current Key : D | | | Print Version : Small | Medium | Large | Intro D G...

Words: 36646 - Pages: 147

Premium Essay

Ssss

...1 Discussion Rubric: Graduate Your active participation in the discussion forums is essential to your overall success this term. Discussion questions are designed to help you make meaningful connections between the course content and the larger concepts and goals of the course. These discussions offer you the opportunity to express your own thoughts, ask questions for clarification, and gain insight from your classmates’ responses and instructor’s guidance. Requirements for Discussion Board Assignments Students are required to post one (1) initial post and to follow up with at least two (2) response posts for each discussion board assignment. For your initial post (1) you must: • Compose a post of one to two paragraphs • Complete the initial post by Thursday at 11:59 PM EST of the module week • Take into consideration material such as course content and other discussion boards from the current module and previous modules, when appropriate For your response posts (2) you must: • Reply to at least two different classmates outside of your own initial post thread • Complete the two response posts by Sunday at 11:59 PM EST • Demonstrate more depth and thought than simply stating “I agree” or “You are wrong.” Guidance is provided for you in each discussion prompt. Rubric |Critical Elements |Distinguished |Proficient |Emerging |Not Evident ...

Words: 525 - Pages: 3

Premium Essay

Ssss

...------------------------------------------------- FINAL EXAM ------------------------------------------------- Fall 2015 ------------------------------------------------- Investments ------------------------------------------------- BUS 315 Student Name: _____________________________Student Number:_________________ DURATION: 3 HOURS No. of Students: 125 Department Name & Course Number: BUSI 315 Section: D100 Course Instructor(s): Yuriy Zabolotnyuk ------------------------------------------------- AUTHORIZED MEMORANDA Financial calculator Students MUST count the number of pages in this examination question paper before beginning to write, and report any discrepancy to a proctor. This question paper has 9 (nine) pages. This examination question paper may not be taken from the examination room. In addition to this question paper, students require: an examination booklet yes Scantron sheet yes ------------------------------------------------- Do ALL 25 multiple choice problems: 2 marks per question for a total of 50 marks. 1) Compared to investing in a single security, diversification provides investors a way to: a) Increase the expected rate of return. b) Decrease the volatility of returns. c) Increase the probability of high returns. d) All of the above 2) In a 5-year period, the annual returns on an investment are 5%, -3%, -4%, 2%, and 6%. The standard deviation of annual returns on this investment...

Words: 2309 - Pages: 10

Premium Essay

Ssss

...modern special report 1 0 1 0 1 28 Ju ly 2 0 1 1 / M o d E R n MaTE Rial S Hand ling 1 11 11 1 00 00 0 11 0 1 00 11 11 0 00 0I 1 00 11 0 10 01 0 11 0 01 1 00 11 0 00 10 1 1 11 000 0 By Bob Trebilcock, Executive Editor f you’re of a certain age, you may remember those crazy bounces you could get from a Super Ball. The image is an apt way of describing the bounce in the market for supply chain management software in 2010. The worldwide market for supply chain management software applications, maintenance and services, or SCM, came in at $6.755 billion in 2010, including applications for procurement software, and nearly $4.6 billion without procurement, according to Chad Eschinger, a research director with Gartner (www.gartner.com). That’s an impressive 10% bounce over 2009. “It was a good, up year,” Eschinger says. That’s a bit of an understatement given that 2009 had posted a 1.9% decline over prior year revenues, the first year-overyear drop in revenues for SCM software since Modern began publishing an annual Top 20 list in 2001. When it comes to market leadership, 2010 reminds us of the lyrics from that old Billie Holliday song, “God Bless The Child:” “Yes, the strong gets more, while the weak ones fade.” The market leaders look much the same as they did in 2009, with SAP ($1.317 billion) and Oracle ($1.21 billion) still at the top of the list and posting impressive gains over 2009. They were followed by JDA Software ($362 million), Manhattan Associates ($136...

Words: 1891 - Pages: 8

Free Essay

Ssss

...Assignment front sheet |Qualification |Unit number and title | |BTEC Level 5 National Diploma |Unit 1: The Hospitality Industry | |Learner name | Assessor name | | |Hong Luco Jingseng | |Date issued |Completion date |Submitted on | |19th September 2014 |10th November 2014 | | | | | |Assignment title |Analyze the business sectors in hospitality industry | | |Grading criteria |Task | | |Criteria...

Words: 1164 - Pages: 5

Premium Essay

Ssss

...UNIT OUTLINE Read this document to learn essential details about your unit. It will also help you to get started with your studies. BMA247 Organisational Behaviour Semester 2, 2015 THIS UNIT IS BEING OFFERED IN: SHANGHAI OCEANS UNIVERSITY (SOU) Taught by: Dr Sarah Dawkins (UTAS Unit Coordinator) & Cindy He (SOU Lecturer) CRICOS Provider Code: 00586B BMA247, Organisational Behaviour 2 Contents Contact Details ........................................................................................................................................ 2 Unit Description ...................................................................................................................................... 3 Prior Knowledge &/or Skills OR Pre-Requisite Unit(s) ............................................................................ 3 Enrolment in the Unit ............................................................................................................................. 3 When does the unit commence? ............................................................................................................ 3 Intended Learning Outcomes and Generic Graduate Attributes for BMA247, Organisational Behaviour ................................................................................................................................................ 4 Learning Expectations and Teaching Strategies/Approach ........................................

Words: 5809 - Pages: 24

Free Essay

Ssss

...ALIF ( ‫) أ‬ 1. Aban : ‫ : َﺑَـﺎن‬perbuatan yang sangat jelas, nama putra khalifah ‫أـ‬ ‘Utsman bin ‘Affan 2. Abiy : ‫ : أﺑﻲ‬yang memiliki kepribadian yang kuat yang pantang ّ َِ tunduk terhadap tekanan 3. Abyan : ‫ : أﺑ َﻦ‬yang lebih jelas ‫َ ْﻴ‬ 4. Adib : ‫ : أدﻳْﺐ‬sastrawan َِ 5. Ahmad : ‫ : أﺣ َﺪ‬yang banyak dipuji-puji, nama yang diberikan oleh Allah ‫َ ْﻤ‬ kepada Nabi Muhammad dalam al-Qur’an 6. Arib : ‫ : أرﻳْﺐ‬yang cerdik dan berakal َِ 7. Arhab : ‫ : أر َﺐ‬yang lapang dada ‫َ ْﺣ‬ 8. Asad : ‫ : أ َﺪ‬singa (lambang keperkasaan) ‫َﺳ‬ 9. Asmar : ‫ : أﺳ َﺮ‬yang berkulilt coklat, abu-abu ‫َ ْﻤ‬ 10. As’ad : ‫ : أﺳ َﺪ‬yang lebih bahagia ‫َ ْﻌ‬ 11. Asyqar : ‫ : أﺷ َﺮ‬yang berambut pirang ‫َ ْﻘ‬ 12. Asyhab : ‫ : أﺷ َﺐ‬warna putih yang bercampur hitam, sebutan lain bagi singa. ‫َ ْﻬ‬ 13. Ashil : ‫ : أﺻﻴْﻞ‬yang asli ِ َ 14. Anis : ‫: َ ِﻴْﺲ‬ ‫أﻧ‬ yang dapat menenangkan hati dari kerisauan/keterasingan 15. Akram : ‫ : أآ َم‬yang lebih mulia ‫َ ْﺮ‬ 16. Aman : ‫ : َ َــﺎن‬rasa aman ‫أﻣ‬ 17. Amin : ‫ : َ ِﻴْــﻦ‬yang dapat dipercaya ‫أﻣ‬ 18. Amir : ‫ : أﻣﻴْﺮ‬Emir, pemimpin, yang memerintahkan َِ 19. Anwar : ‫ : أﻧﻮر‬yang lebih bercahaya َ َْ 20. Arkan : ‫ : أر َﺎن‬pondasi, pokok ‫َ ْآ‬ 21. Awwab : ‫ : أ ﱠاب‬yang amat taat kepada Tuhan, julukan bagi nabi Daud ‫َو‬ 'alaihissalam 22. Ayib : ‫ : ﺁ ِﺐ‬yang kembali ‫ﻳ‬ 23. Ayyub : ‫ : أﻳﻮْب‬yang banyak kembali, nama nabi ‫َﱡ‬ 24. Islam : ‫ : إﺳﻠﺎم‬keislaman َْ ِ 25. I’tisham : ‫ :اﻋﺘ َﺎم‬berpegang teguh ‫ِ ْ ِﺼ‬ 26. Iklil : ‫ : إآﻠﻴْﻞ‬mahkota...

Words: 5438 - Pages: 22