XML Programming JAXP Andy Clark 17 Apr 2002
- Slides: 11
XML Programming: JAXP Andy Clark 17 Apr 2002
Java API for XML Processing l Standard Java API for loading, creating, accessing, and transforming XML documents – JAXP: parsing l – Tr. AX: transformation l l Supports DOM and SAX Generic transformation framework Java standard extension (i. e. javax package) – Defined by Sun JCP
JAXP Implementation l JAXP is a set of defined interfaces – l Parsers distribute implementation configured to return appropriate parser instances Search method for finding implementation – – – System property: “javax. xml. parsers. Document. Builder. Factory” JAXP properties: “%JAVA_HOME%/lib/jaxp. properties” Jar services: “META_INF/services/javax. xml. parsers. Document. Builder. Factory”
javax. xml. parsers l DOM – l SAX – l Document. Builder, Document. Builder. Factory SAXParser, SAXParser. Factory General – – Factory. Configuration. Error Parser. Configuration. Exception
DOM: Parsing Document l Get instance of document builder factory – – – l Create document builder – l Document. Builder. Factory factory = Document. Builder. Factory. new. Instance(); factory. set. Namespace. Aware(true); factory. set. Validating(false); Document. Builder builder = factory. new. Document. Builder(); Parse document – Document document = builder. parse(“document. xml”);
DOM: Creating Empty Document l Get instance of document builder factory – l Create document builder – l Document. Builder. Factory factory = Document. Builder. Factory. new. Instance(); Document. Builder builder = factory. new. Document. Builder(); Create document – Document document = builder. new. Document();
SAX: Parsing Document l Get instance of SAX parser factory – – – l Create SAX parser – l SAXParser. Factory factory = SAXParser. Factory. new. Instance(); factory. set. Namespace. Aware(true); factory. set. Validating(false); SAXParser parser = factory. new. SAXParser(); Parse document – parser. parse(“document. xml”, new My. Handler());
Configuration l Document. Builder. Factory – Allows setting of “attributes” l l l String identifier Object value SAXParser. Factory – Allows setting of “features” l l – String identifier boolean state SAXParser allows setting of “properties” l String identifier, Object value
Error Handling and Entity Resolution l Document. Builder – – l Can set a SAX Error. Handler Can set a SAX Entity. Resolver SAXParser – Must query XMLReader first, then l l Can set Error. Handler Can set Entity. Resolver
Useful Links l JAXP – l http: //java. sun. com/xml/ Apache Xerces XML Parser – http: //xml. apache. org/xerces 2 -j/
XML Programming: JAXP Andy Clark