1 / 75

Processing XML with Java

Processing XML with Java. A comprehensive tutorial about XML processing with Java XML tutorial of W3Schools. Parsers. What is a parser?. Formal grammar. Analyzed Data. Input. Parser.

lael
Télécharger la présentation

Processing XML with Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Processing XML with Java A comprehensive tutorial about XML processing with Java XML tutorial of W3Schools

  2. Parsers • What is a parser? Formal grammar Analyzed Data Input Parser The structure(s) of the input, according to the atomic elements and their relationships (as described in the grammar)

  3. XML-Parsing Standards • We will consider two parsing methods that implement W3C standards for accessing XML • DOM • convert XML into a tree of objects • “random access” protocol • SAX • “serial access” protocol • event-driven parsing

  4. XML Examples

  5. root element world.xml <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6,199,008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60,424,213</population> </country> </countries> validating DTD file reference to an entity

  6. XML Tree Model element element attribute simple content

  7. world.dtd <!ELEMENTcountries(country*)> <!ELEMENTcountry(name,population?,city*)> <!ATTLISTcountrycontinentCDATA#REQUIRED> <!ELEMENTname (#PCDATA)> <!ELEMENTcity (name)> <!ATTLISTcitycapital(yes|no) "no"> <!ELEMENTpopulation (#PCDATA)> <!ATTLISTpopulationyearCDATA#IMPLIED> <!ENTITYeu"Europe"> <!ENTITYas"Asia"> <!ENTITY af"Africa"> <!ENTITY am"America"> <!ENTITY au"Australia"> parsed Not parsed default value As opposed to required Open world.xml in your browser Check world2.xml for #PCDATA exmaple

  8. Namespaces sales.xml <?xml version="1.0"?> <forsale date="12/2/03" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <book> <title> <xhtml:em>DBI:</xhtml:em> <![CDATA[Where I Learned <xhtml>.]]> </title> <comment xmlns="http://www.cs.huji.ac.il/~dbi/comments"> <par>My <xhtml:b> favorite </xhtml:b> book!</par> </comment> </book> </forsale> “xhtml” namespace declaration (non-parsed) character data namespace overriding default namespace declaration

  9. sales.xml <?xml version="1.0"?> <forsale date="12/2/03" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <book> <title> <xhtml:h1> DBI </xhtml:h1> <![CDATA[Where I Learned <xhtml>.]]> </title> <comment xmlns="http://www.cs.huji.ac.il/~dbi/comments"> <par>My <xhtml:b> favorite </xhtml:b> book!</par> </comment> </book> </forsale> Namespace:“http://www.w3.org/1999/xhtml” Local name: “h1” Qualified name:“xhtml:h1”

  10. sales.xml <?xml version="1.0"?> <forsale date="12/2/03" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <book> <title> <xhtml:h1> DBI </xhtml:h1> <![CDATA[Where I Learned <xhtml>.]]> </title> <comment xmlns="http://www.cs.huji.ac.il/~dbi/comments"> <par>My <xhtml:b> favorite </xhtml:b> book!</par> </comment> </book> </forsale> Namespace:“http://www.cs.huji.ac.il/~dbi/comments” Local name:“par” Qualified name:“par”

  11. sales.xml <?xml version="1.0"?> <forsale date="12/2/03" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <book> <title> <xhtml:h1>DBI</xhtml:h1> <![CDATA[Where I Learned <xhtml>.]]> </title> <comment xmlns="http://www.cs.huji.ac.il/~dbi/comments"> <par>My <xhtml:b> favorite </xhtml:b> book!</par> </comment> </book> </forsale> Namespace: “” Local name:“title” Qualified name:“title”

  12. sales.xml <?xml version="1.0"?> <forsale date="12/2/03" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <book> <title> <xhtml:h1>DBI</xhtml:h1> <![CDATA[Where I Learned <xhtml>.]]> </title> <comment xmlns="http://www.cs.huji.ac.il/~dbi/comments"> <par>My <xhtml:b> favorite </xhtml:b> book!</par> </comment> </book> </forsale> Namespace:“http://www.w3.org/1999/xhtml” Local name:“b” Qualified name:“xhtml:b”

  13. DOM – Document Object Model

  14. DOM Parser • DOM = Document Object Model • Parser creates a tree object out of the document • User accesses data by traversing the tree • The tree and its traversal conform to a W3C standard • The API allows for constructing, accessing and manipulating the structure and content of XML documents

  15. <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6,199,008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60,424,213</population> </country> </countries>

  16. The DOM Tree

  17. Application API XML File DOM Parser DOM Tree Using a DOM Tree in memory

  18. Creating a DOM Tree • A DOM tree is generated by a DocumentBuilder • The builder is generated by a factory, in order to be implementation independent • The factory is chosen according to the system configuration DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("world.xml");

  19. Configuring the Factory • The methods of the document-builder factory enable you to configure the properties of the document building • For example • factory.setValidating(true) • factory.setIgnoringComments(false) Read more about DocumentBuilderFactory Class, DocumentBuilder Class

  20. The Node Interface • The nodes of the DOM tree include • a special root (denoted document) • The Document interface retrieved by builder.parse(…) actually extends the Node Interface • element nodes • text nodes and CDATA sections • attributes • comments • and more ... • Every node in the DOM tree implements the Node interface This is very unintuitive (some would even say this is a bloated design)

  21. Figure as appears in : “The XML Companion” - Neil Bradley NodeList NamedNodeMap A light-weight fragment of the document. Can hold several sub-trees Interfaces in a DOM Tree DocumentFragment Document Text CDATASection CharacterData Comment Attr Element Node DocumentType Notation Entity EntityReference Run FragmentVsElement with 1st argument fragment/element ProcessingInstruction DocumentType

  22. Document Type Element Attribute Attribute Element Element Text Text Element Entity Reference Text Text Comment Text Interfaces in the DOM Tree Document

  23. Node Navigation • Every node has a specific location in tree • Node interface specifies methods for tree navigation • Node getFirstChild(); • Node getLastChild(); • Node getNextSibling(); • Node getPreviousSibling(); • Node getParentNode(); • NodeList getChildNodes(); • NamedNodeMap getAttributes()

  24. Node Navigation (cont) getPreviousSibling() getFirstChild() getChildNodes() getParentNode() getLastChild() getNextSibling()

  25. Not a very OO approach… reminds one of simulating OO using unions in C…. Node Properties Only Element Nodes have attributes… some would say this should’ve been a property of the Element derived class and not of Node. • Every node has (a dubious design and…) • a type • a name • a value • attributes • The roles of these properties differ according to the node types • Nodes of different types implement different interfaces (that extend Node) Very few nodes have both a significant name and a significant value

  26. Names, Values and Attributes

  27. Node Types - getNodeType() ELEMENT_NODE = 1 ATTRIBUTE_NODE = 2 TEXT_NODE = 3 CDATA_SECTION_NODE = 4 ENTITY_REFERENCE_NODE = 5 ENTITY_NODE = 6 PROCESSING_INSTRUCTION_NODE = 7 COMMENT_NODE = 8 DOCUMENT_NODE = 9 DOCUMENT_TYPE_NODE = 10 DOCUMENT_FRAGMENT_NODE = 11 NOTATION_NODE = 12 if (myNode.getNodeType() == Node.ELEMENT_NODE) { //process node … } Read more about Node Interface

  28. import org.w3c.dom.*; import javax.xml.parsers.*; publicclass EchoWithDom { publicstaticvoid main(String[] args) throwsException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(“world.xml"); new EchoWithDom().echo(doc); } e.g white spaces used for indentation in non mixed data elements

  29. privatevoid echo(Node n) { print(n); if (n.getNodeType() == Node.ELEMENT_NODE) { NamedNodeMap atts = n.getAttributes(); ++depth; for (int i = 0; i < atts.getLength(); i++) echo(atts.item(i)); --depth; } depth++; for (Node child = n.getFirstChild(); child != null; child = child.getNextSibling()) echo(child); depth--; } Attribute nodes are not included…

  30. privateint depth = 0; privateString[] NODE_TYPES = { "", "ELEMENT", "ATTRIBUTE", "TEXT", "CDATA", "ENTITY_REF", "ENTITY", "PROCESSING_INST", "COMMENT", "DOCUMENT", "DOCUMENT_TYPE", "DOCUMENT_FRAG", "NOTATION" }; privatevoid print(Node n) { for (int i = 0; i < depth; i++) System.out.print(" "); System.out.print(NODE_TYPES[n.getNodeType()] + ":"); System.out.print("Name: "+ n.getNodeName()); System.out.print(" Value: "+ n.getNodeValue()+"\n"); }} run EchoWithDom, pay attention to the default values

  31. Another Example publicclass WorldParser { publicstaticvoid main(String[] args) throwsException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("world.xml"); printCities(doc); }

  32. Another Example (cont) publicstaticvoid printCities(Document doc) { NodeList cities = doc.getElementsByTagName("city"); for(int i=0; i<cities.getLength(); ++i) { printCity((Element)cities.item(i)); } } publicstaticvoid printCity(Element city) { Node nameNode = city.getElementsByTagName("name").item(0); String cName = nameNode.getFirstChild().getNodeValue(); System.out.println("Found City: " + cName); } Searches within descendents run WorldParser

  33. Normalizing the DOM Tree • Normalizing a DOM Tree has two effects: • Combine adjacent textual nodes • Eliminate empty textual nodes • To normalize, apply the normalize() method to the document element Created by node manipulation…

  34. Node Manipulation • Children of a node in a DOM tree can be manipulated - added, edited, deleted, moved, copied, etc. • To constructs new nodes, use the methods of Document • createElement,createAttribute, createTextNode, createCDATASection etc. • To manipulate a node, use the methods of Node • appendChild, insertBefore, removeChild,replaceChild, setNodeValue, cloneNode(boolean deep)etc.

  35. Figure as appears in “The XML Companion” - Neil Bradley Old New New Ref insertBefore replaceChild deep = 'false' cloneNode deep = 'true' Node Manipulation (cont)

  36. SAX – Simple APIfor XML

  37. SAX Parser • SAX = Simple API for XML • XML is read sequentially • When a parsing event happens, the parser invokes the corresponding method of the corresponding handler. • This is called event-driven programming. Most GUI programs are written using this paradigm. • The handlers are programmer’s implementation of standard Java API (i.e., interfaces and classes)

  38. <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <!--israel--> <name>Israel</name> <population year="2001">6,199,008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60,424,213</population> </country> </countries>

  39. <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <!--israel--> <name>Israel</name> <population year="2001">6,199,008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60,424,213</population> </country> </countries> Start Document

  40. <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <!--israel--> <name>Israel</name> <population year="2001">6,199,008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60,424,213</population> </country> </countries> Start Element

  41. <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <!--israel--> <name>Israel</name> <population year="2001">6,199,008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60,424,213</population> </country> </countries> Start Element

  42. <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <!--israel--> <name>Israel</name> <population year="2001">6,199,008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60,424,213</population> </country> </countries> Comment

  43. <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <!--israel--> <name>Israel</name> <population year="2001">6,199,008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60,424,213</population> </country> </countries> Start Element

  44. <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <!--israel--> <name>Israel</name> <population year="2001">6,199,008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60,424,213</population> </country> </countries> Characters

  45. <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <!--israel--> <name>Israel</name> <population year="2001">6,199,008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60,424,213</population> </country> </countries> End Element

  46. <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <!--israel--> <name>Israel</name> <population year="2001">6,199,008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60,424,213</population> </country> </countries> End Element

  47. <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <!--israel--> <name>Israel</name> <population year="2001">6,199,008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60,424,213</population> </country> </countries> End Document

  48. <?xml version="1.0"?> . . . SAX Parsers When you see the start of the document do … SAX Parser When you see the start of an element do … When you see the end of an element do …

  49. Used to create a SAX Parser Handles document events: start tag, end tag, etc. Handles Parser Errors Handles DTD Handles Entities

More Related