Processing of structured documents Part 4 XML processing


















![Character data public void characters (char[] ch, int start, int end){ } String s Character data public void characters (char[] ch, int start, int end){ } String s](https://slidetodoc.com/presentation_image/63be210f29d28ea7d35fccc92ac60c4c/image-19.jpg)

























- Slides: 44
Processing of structured documents Part 4
XML processing model z. XML processor is used to read XML documents and provide access to their content and structure z. XML processor works for some application zthe XML specification defines which information the processor should provide to the application
Parsing zinput: an XML document zbasic task: is the document well-formed? zvalidating parsers additionally: is the document valid?
Parsing zparsers produce data structures, which other tools and applications can use ztwo kind of APIs: tree-based and eventbased
Tree-based API zcompiles an XML document into an internal tree structure zallows an application to navigate the tree z. Document Object Model (DOM) is a treebased API for XML and HTML documents
Event-based API zreports parsing events (such as start and end of elements) directly to the application zthe application implements handlers to deal with the different events z. Simple API for XML (SAX)
Example <? xml version=” 1. 0”> <doc> <para>Hello, world!</para> </doc> z. Events: start document start element: doc start element: para characters: Hello, world! end element: para end element: doc end document
Example (cont. ) zan application handles these events just as it would handle events from a graphical user interface (mouse clicks, etc) as the events occur zno need to cache the entire document in memory or secondary storage
Tree-based vs. event-based ztree-based APIs are useful for a wide range of applications, but they may need a lot of resources (if the document is large) zsome applications may need to build their own tree structures, and it is very inefficient to build a parse tree only to map it to another tree
Tree-based vs. event-based zan event-based API is simpler, lower-level access to an XML document zas document is processed sequentially, one can parse documents much larger than the available system memory zown data structures can be constructed using own callback event handlers
SAX z. A parser is needed ye. g. Apache Xerces: http: //xml. apache. org zand SAX classes ywww. saxproject. org yoften the SAX classes come bundled to the parser distribution
Starting a SAX parser import org. xml. sax. XMLReader; import org. apache. xerces. parsers. SAXParser; XMLReader parser = new SAXParser(); parser. parse(uri);
Content handlers z. In order to let the application do something useful with XML data as it is being parsed, we must register handlers with the SAX parser zhandler is a set of callbacks: application code can be run at important events within a document’s parsing
Core handler interfaces in SAX zorg. xml. sax. Content. Handler zorg. xml. sax. Error. Handler zorg. xml. sax. DTDHandler zorg. xml. sax. Entity. Resolver
Custom application classes zcustom application classes that perform specific actions within the parsing process can implement each of the core interfaces zimplementation classes can be registered with the parser with the methods set. Content. Handler(), etc.
Example: content handlers class My. Content. Handler implements Content. Handler { public void start. Document() { System. out. println(”Parsing begins…”); } public void end. Document() { System. out. println(”. . . Parsing ends. ”); }
Element handlers public void start. Element (String namespace. URI, String local. Name, String raw. Name, Attributes atts) { System. out. print(”start. Element: ” + local. Name); if (!namespace. URI. equals(””)) { System. out. println(” in namespace ” + namespace. URI + ” (” + rawname + ”)”); } else { System. out. println(” has no associated namespace”); } for (int i=0; i<atts. get. Length(); i++) { System. out. println(” Attribute: ” + atts. get. Local. Name(i) + ”=” + atts. get. Value(i)); }}
end. Element public void end. Element(String namespace. URI, String local. Name, String raw. Name) { } System. out. println(”end. Element: ” + local. Name + ”n”);
Character data public void characters (char[] ch, int start, int end){ } String s = new String(ch, start, end); System. out. println(”characters: ” + s); zparser may return all contiguous character data at once, or split the data up into multiple method invocations
Processing instructions z. XML documents may contain processing instructions (PIs) za processing instruction tells an application to perform some specific task zform: <? target instructions? >
Handlers for PIs public void processing. Instruction (String target, String data){ } System. out. println(”PI: Target: ” + target + ” and Data: ” + data); z. Application could receive instructions and set variables or execute methods to perform applicationspecific processing
Validation zsome parsers are validating, some nonvalidating zsome parsers can do both z. SAX method to turn validation on: parser. set. Feature (”http: //xml. org/sax/features/validation”, true);
Ignorable whitespace zvalidating parser can decide which whitespace can be ignored zfor a non-validating parser, all whitespace is just characters zcontent handler: public void ignorable. Whitespace (char[] ch, int start, int end) { … }
Traversing XML: DOM z. In transforming documents, random access to a document is needed z. SAX cannot look backward or forward zdifficult to locate siblings and children z. DOM: access to any part of the tree ywww. w 3. org/DOM/
DOM z. Level 1: navigation of content within a document z. Level 2: modules and options for specific content models, such as XML, HTML, and CSS; events z. Level 3: document loading and saving; access of schemas
Some requirements z. All document content, including elements and attributes, will be programmatically accessible and manipulable z. Navigation from any element to any other element will be possible z. There will be a way to add, remove, and change elements/attributes in the document structure
DOM z. XML documents are treated as a tree of nodes zevery item is a node zchild elements and enclosed text are subnodes
XML DOM objects z. Element z. Attr z. Text z. CDATAsection z. Entity. Reference z. Entity z. Document z. . .
Node-related objects z. Node ya single node in the document tree z. Node. List ya list of node objects (e. g. children) z. Named. Node. Map yallows access by name to the collection of attributes
DOM Java bindings z. DOM is language-neutral z. Java bindings y. Interfaces and classes that define and implement the DOM ybindings often included in the parser implementations (the parser generates a DOM tree)
Parsing using a DOM parser Import org. w 3 c. dom. *; import org. apache. xerces. parsers. DOMParser; DOMParser parser = new DOMParser(); parser. parse(uri);
Output tree zthe entire document is parsed and added into the output tree, before any processing takes place zhandle: org. w 3 c. dom. Document object = one level above the root element in the document parser. parse(uri); Document doc = parser. get. Document();
Printing a document Private static void print. Tree(Node node) { switch (node. get. Node. Type()) { case Node. DOCUMENT_NODE: // Print the contents of the Document object break; case Node. ELEMENT_NODE: // Print the element and its attributes break; case Node. TEXT_NODE: . . .
…the Document node Case Node. DOCUMENT_NODE: System. out. println(”<xml version=” 1. 0”>n”); Document doc = (Document)node; print. Tree(doc. get. Document. Element()); break;
… elements Case Node. ELEMENT_NODE: String name= node. get. Node. Name(); System. out. print(”<” + name); // Print out attributes… (see next slide…) System. out. println(”>”); // recurse on each child Node. List children = node. get. Child. Nodes(); if (children != null) { for (int i=0; i<children. get. Length(); i++) { print. Tree(children. item(i)); } } System. out. println(”</” + name + ”>”);
… and their attributes case Node. ELEMENT_NODE: String name = node. get. Node. Name(); System. out. print(”<” + name); Named. Node. Map attributes = node. get. Attributes(); for (int i=0; i<attributes. get. Length(); i++) { Node current = attributes. item(i); System. out. print(” ” + current. get. Node. Name() + ”=”” + current. get. Node. Value() + ”””); } System. out. println(”>”); . . .
…textual nodes case Node. TEXT_NODE: case Node. CDATA_SECTION_NODE: System. out. print(node. get. Node. Value()); break;
Document interface methods z. Attr create. Attribute(String name) z. Element create. Element(String tag. Name) z. Text create. Text. Node(String data) z. Element get. Document. Element() z. Element get. Element. By. Id(String element. ID) z. Node. List get. Elements. By. Tag. Name(String tag. Name)
Node. List interface methods zint get. Length() ygets the number of nodes in this list z. Node item(int index) ygets the item at the specified index value in the collection
Node interface methods z. Named. Node. Map get. Attributes() z. Node. List get. Child. Nodes() z. String get. Local. Name() z. String get. Node. Value() z. Node get. Parent. Node() zshort get. Node. Type() zappend. Child()
Node types zstatic zstatic z. . . short short ATTRIBUTE_NODE ELEMENT_NODE TEXT_NODE DOCUMENT_NODE COMMENT_NODE
Element interface methods z. String get. Attribute() yreturns an attribute’s value z. String get. Tag. Name() yreturn an element’s name zremove. Attribute() y removes an element’s attribute zset. Attribute() yset an attribute’s value
Attr interface methods z. String get. Name() ygets the name of this attribute z. Element get. Owner. Element() ygets the Element node to which this attribute is attached z. String get. Value() ygets the value of the attribute as a string
Named. Node. Map interface methods z. Int get. Length() yreturns the number of nodes in this map z. Node get. Named. Item(String name) ygets a node indicated by name z. Node item(int index) ygets an item in the map by index












































