1 / 35

Chapter 9 Java and XML

Chapter 9 Java and XML. x.zhang@seu.edu.cn. Content. XML Pilot http://www.w3school.com.cn/ Parsing XML Document DOM Objects in DOM Java Programming using DOM. XML Pilot. XML 可扩展标记语言 eXtensible Markup Language Motivate: Data Exchange. XML Text. Student Info System.

milek
Download Presentation

Chapter 9 Java and XML

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. Chapter 9 Javaand XML x.zhang@seu.edu.cn

  2. Content • XML Pilot http://www.w3school.com.cn/ • Parsing XMLDocument • DOM • Objects in DOM • Java Programming using DOM COSE Java 2009

  3. XMLPilot • XML 可扩展标记语言 • eXtensible Markup Language • Motivate: Data Exchange XMLText Student Info System Student Marks System Data Exchange Student Info Database Student Marks Database

  4. XMLPilot • Origin • SGML 标准通用化标记语言 • Powerful • Extensible • Expensive • HTML 超文本标记语言 • Limited function • Not extensible • Free Java CoSE 2009

  5. XML Pilot– An Example • Johnwrote to Georgein XML • An XMLdocuments includes at least: • An XMLdeclaration • An XMLroot element

  6. XMLPilot– An Example

  7. XMLPilot– XMLTree Structure

  8. XML vs. HTML • All XML element must have a close tag • XML tag is case-sensitive • XML element must be properly nested Java CoSE 2009

  9. XML vs. HTML • XML document must have an root element • XML attribute must be quoted Java CoSE 2009

  10. Entity Reference and Comments • In XML, some characters have special meaning • Five reserved Entity Reference • XMLcomments Java CoSE 2009

  11. XML Element • XMLElement can have • Sub-element • Attribute • Text • Naming of elements • Letters, numbers and other char • Cannot begin with figure, or punctuation, and “XML” • Cannot contain spaces Java CoSE 2009

  12. CDATA • Long text in XMLcan be represented in CDATA • All content in CDATA will be interpreted as text • No entity reference in CDATA • CDATAgrammar: • Begin with “<!CDATA[” • End with “]]>” Java CoSE 2009

  13. Well-formed XML • A “Well-formed” XML should be: • XML document having root element • All elements being properly closed • Case-sensitive • All elements being properly nested • All attributes being properly quoted Java CoSE 2009

  14. Validated XML • Well-formed XML only indicates that the format of XML document is correct • We still need a way to check whether the data structure in XML document is right too • Each book price in bookstore should be positive float • Each book contains at least one title • The way to validate XML • DTD (Document Type Definition) • XML Schema Java CoSE 2009

  15. namespace • Name confliction may happen when two XML contains elements with same name: Java CoSE 2009

  16. namespace • Using namespace to avoid naming confliction Java CoSE 2009

  17. DOM • Document Object Model 文档对象模型 • DOM is • Programming interface for handling XML document • Define how to visit and manipulate XML document • Represent XML document in a tree structure • Feature • Independent with platform • Independent with language

  18. DOMObject • org.w3c.dom.Document // XMLdoc entrance • org.w3c.dom.Node// nodes in XML doc • org.w3c.dom.Element// element node in XMLdoc • org.w3c.dom.Attr // attribute node in XMLdoc • org.w3c.dom.Text// text node in XML doc • org.w3c.dom.NodeList// ordered nodelist in XMLdoc • org.w3c.dom.NamedNodeMap// a mapping between node and node name

  19. DOM Tree

  20. DOM树中的对象 #document Document Element Node Attribute NodeList Text

  21. NodeInterface • An interface for all nodes in XMLdoc tree • Derive all other interfaces in DOM • The type of a node is marked in nodetype • Method • getNodeType() /NodeName() / NodeValue() • getParentNode() / getChildNodes() • getFirstChild () /getLastChild() / getChild • getNextSibling()

  22. DocumentInterface • Inherited from Nodeinterface • A virtual root element– the entrance of XML doc • Method • getElementsByTagName(String tagname) • getDocumentElement() • createElement(String tagName) • createAttribute(String name) • createTextNode(String data)

  23. ElementInterface • Inherited from NodeInterface • Representing XMLelements • Method • getAttributes() / setAttribute() • getTextContent() / setTextContent() • getOwnerDocument()

  24. AttrInterface • Inherited from NodeInterface • Representing XMLattributes • Methods • getOwnerElement() • getSpecified() • getValue() / setValue()

  25. NodeListInterface • Ordered list of nodes • For example: an ordered list of all nodes derived by getChildNodes() of book element • Methods • getLength() • item(int index)

  26. NamedNodeMapInterface • A Mapping between nodes and node names • For example: a collection of all attributes derived by getAttributes() of an element • Methods • getNamedItem(String name) • setNamedItem(Node node)

  27. JAXPImplementing DOM • JAXP : Java API for XML Parsing • Using JAXP to parse XML Java CoSE 2009

  28. Episode– Factory Pattern • Sample s = new Sample(); • If Sampleis an interface, then • Sample s1 = new MySample1(); • Sample s2 = new MySample2(); • Using factory pattern Java CoSE 2009

  29. Create a DOM Tree in Memory

  30. Create a DOM Tree in Memory

  31. Create a DOM Tree in Memory

  32. Create a DOM Tree in Memory

  33. Create a DOM Tree in Memory

  34. Think: How to Traverse XML Nodes Java CoSE 2009

  35. Self-study • XML Document Model • DOM • SAX • XML Parser • JAXP • Xerces • JDOM • DOM4J Java CoSE 2009

More Related