home news blogs forums events research newsletter whitepapers careers


Network Computing Network Computing Powered by InformationWeek Business Technology Network

IMMERSE YOURSELF:

SOA

  |

Data Center

  |

802.11n

  |

Data Privacy

  |
APO  |

Virtualization

  |

NAC

  |

Security

  |

Network Mgmt

  |

Enterprise Apps

  |

Storage & Servers



Netdesign Manual

Part 2

Java XML Programmers Reference

Chapter 11: XML Tools for Information Appliances


September 24, 2001


Brought to you by:





Check It Out!

Class StdXMLReader
net.n3.nanoxml

public class StdXMLReader

extends java.lang.Object

implements net.n3.nanoxml.IXMLReader

The StdXMLReader class implements IXMLReader, an interface called by StdXMLParser to read XML data from a data source. It is the default implementation of IXMLReader used by XMLParserFactory.

Since you probably won't be implementing your own reader or parser, we'll only discuss two methods in this class. They allow you to choose one of two different data sources: java.io.Reader or java.lang.String.

public StdXMLReader(Reader reader)

public static IXMLReader stringReader(String str)

The first method, the constructor, is the one to use to read from a Reader. The second, static method, stringReader(), is the method to use to read from a String.

Class StdXMLParser
net.n3.nanoxml

public class StdXMLParser

extends java.lang.Object

implements net.n3.nanoxml.IXMLParser

The StdXMLParser class implements the IXMLParser interface. This interface is provided so that you can write your own parser and plug it into your application, if needed. StdXMLParser is a default implementation and is used by XMLParserFactory.

Unless you plan on implementing your own parser that meets the IXMLParser contract, you should use instances of StdXMLParser. Here are its methods:

public StdXMLParser()
public void setBuilder(IXMLBuilder builder)
public void setReader(IXMLReader reader)
public void setValidator(IXMLValidator validator)
public Object parse() 
throws IOException 
You really will not need to call any of these methods unless you:
  • Do not want to use the default parser class (StdXMLParser) when calling XMLParserFactory for parser instances

  • Want to avoid using XMLParserFactory altogether (as discussed in the section Class XMLParserFactory, page 608)
The parse() method returns the top-most XMLElement after parsing completes. You can safely narrow the object returned by parse() to an XMLElement. Here are the arguments.

Arguments

Arguments Type Effect
builder net.n3.nanoxml.
IXMLBuilder
The object that builds a tree of XMLElement nodes from a data source
reader net.n3.nanoxml.
IXMLReader
The objects that reads the data to be parsed
validator net.n3.nanoxml.
IXMLValidator
The object that processes the DTD and resolves entity references. No document validation is performed!
Usage and Examples

Here is an example that parses a document using the XMLParserFactory and StdXMLParser classes.

StdXMLBuilder builder = new StdXMLBuilder();
IXMLParser parser = XMLParserFactory.createDefaultXMLParser(builder, 
new StdXMLReader(new BufferedReader(
new FileReader("request.xml"))), new NonValidator());
XMLElement root = (XMLElement)parser.parse();


Class XMLWriter
net.n3.nanoxml

public class XMLWriter
extends java.lang.Object

The XMLWriter class is used to output an XML document. In version 1.6.7, this functionality was included in the XMLElement class as write() methods. In version 2.0, however, a new class has been created to handle output, making the library more object-oriented.

public XMLWriter(Writer writer)

public XMLWriter(OutputStream st)

public void write(XMLElement elem)

public void write(XMLElement elem, int indent)

The entire document, or a document fragment, can be written to any class extending java.io.Writer or java.io.OutputStream. There are no output options; all attributes and #PCDATA are always output for each element.

Arguments

Arguments Type Effect
writer java.io.
Writer
The writer object to which output is written
st java.io.
OutputStream
The stream to which output is written
elem net.n3.nanoxml.
XMLElement
The element to output. Its children are also output.
indent int Number of spaces to indent for each new child of element
Usage and Examples

We'll use the example we've been building upon to write a full application that parses an XML document called request.xml and echoes its contents to System.out:

import net.n3.nanoxml.*;
import java.io.*;
public class Echo {
public Echo() throws Exception {
StdXMLBuilder builder = new StdXMLBuilder();
IXMLParser parser = 
XMLParserFactory.createDefaultXMLParser(builder,
new StdXMLReader(new BufferedReader(new
FileReader("request.xml"))), new NonValidator());
XMLElement root = (XMLElement)parser.parse();
//create a writer and output the document to System.out
XMLWriter writer = new XMLWriter(System.out);
writer.write(root);
}
public static void main(String[] args) throws Exception {
Echo e = new Echo();
}
}


Coming Up Next: MinML

PAGE: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FIRST PAGE
 





Ready to take that job and shove it?

Function:

Keyword(s):

State:
SPONSOR
RECENT JOB POSTINGS
CAREER NEWS
Go beyond Google and get vertical. These specialized search sites will help you find the business information you need -- fast.

Ari Balogh was named to the post of chief technology officer as the companys for a "realignment" of employees.










InformationWeek U.S. IT Salary Survey 2008
Salaries for business technology professionals are falling. Here's what you need to know in order to make good hiring decisions and personal career choices. Download Today
 
ROLLING RIGHT ALONG
Follow key Network Computing Reviews from conception to completion. This Week: Holistic APM.



Network Computing Reports Emerging Enterprise Podcast Series: Secrets to Success








TechSearch


Microsite of the Week


Powerful Information at Your Fingertips



InformationWeek Business Technology Network
InformationWeekInformationWeek 500InformationWeek 500 ConferenceInformationWeek AnalyticsInformationWeek CIO
InformationWeek EventsInformationWeek ReportsInformationWeek MagazinebMightyByte and SwitchDark Reading
Digital LibraryIntelligent EnterpriseInternet EvolutionNetwork ComputingNo Jitter
space
Techweb Events Network
InteropVoiceConWeb 2.0 ExpoWeb 2.0 SummitEnterprise 2.0 ConferenceMobile Business ExpoSoftware ConferenceCSI - Computer Security Institute
Black HatGTECEnergy CampMashup CampStartup Camp
space
Light Reading Communications Network
Light ReadingLight Reading EuropeUnstrungLight Reading's Cable Digital NewsConstantinopleInternet Evolution
Heavy ReadingLight Reading Live!Light Reading InsiderEthernet ExpoOptical ExpoTeleco TVTower Technology Summit
space
Financial Technology Network
Advanced TradingBank Systems & TechnologyInsurance & TechnologyWall Street & TechnologyAccelerating Wall StreetBank Systems & Technology Executive SummitBuyside Trading SummitInsurance & Technology Executive Summit
space
Microsoft Technology Network
MSDN MagazineTechNetThe Architecture Journal
space
App Infrastructure   |   Messaging & Collaboration   |   Network & Systems Mgmt   |   Network Infrastructure   |   Security  |   Storage & Servers   |   Wireless   |   Enterprise Apps
About Us  |  Contact Us  |  Site Map  |  Technology Marketing Solutions  |  Advertising Contacts  |   Briefing Centers
Copyright © 2008  United Business Media LLC  |  Privacy Statement  |  Terms of Service  |  Your California Privacy Rights