Chapter 11.5 XML and more A Brief Introduction to XML • XML (eXtensible Markup Language) is a syntax for creating data representation languages. • There are XML languages for representing mathematical expressions (MathML), musical notation (MusicXML), molecules and chemical reactions (CML), vector graphics (SVG), and many other kinds of information. Basic XML Syntax • If you know HTML, the language for writing document looks a lot like an HTML document. HTML is not itself an XML language, since it does not follow all the strict XML syntax rules, but the basic ideas are similar. • A document is made up of elements, attributes, and textual content. Elements, Textual Content, Attributes • An element starts with a tag, such as <curve> and ends with a matching end-tag such as </curve>. • Between the tag and end-tag is the textual content of the element, which can consist of text and nested elements. • If an element has no content, then the opening tag and end-tag can be combined into a single empty tag (which ends with “/>”.) • A tag can include attributes. XML Syntax Rules • Tag names and attribute names in XML are case sensitive. • A name must begin with a letter and can contain letters, digits and certain other characters. • Spaces and ends-of-line are significant only in textual content. • Every tag must either be an empty tag or have a matching end-tag. • A document must have a root element , which contains all the other elements. • Every attribute must have a value, and that value must be enclosed in quotation marks (“ or ‘) • & is an escape char: &lt, &amp, &gt, &apos Working With the DOM • Java has a standard API for parsing and processing XML Documents. • Document Object Model , or DOM specifies how to build data structures to represent XML documents, and it specifies some standard methods for accessing the data in that structure. • The data structure is a kind of tree whose structure mirrors the structure of the document. • In Java, the DOM representation of an XML document file can be created with just two statements. • Classes defined in the package javax.xml.parsers. • The structure of the DOM data structure is defined in the package org.w3c.dom. • An object of type Document represents an entire XML document. The return value of docReader.parse() — xmldoc in the above example—is of type Document. xmldoc.getDocumentElement() • element.getTagName() String: tag’s name • element.getAttribute(attrName) String: attr value • element.getTextContent() String: textual content • element.getChildNodes() NodeList: child nodes • element.getChildNodes() Nodelist: nodes representing all elements that are nested inside element and which have the given tag name. <background red=’255’ green=’153’ blue=’51’/> • This element might be encountered either while traversing the document with getChildNodes() or in the result of a call to getElementsByTagName("background"). • int r = Integer.parseInt( element.getAttribute("red") ); • int g = Integer.parseInt( element.getAttribute("green") ); • int b = Integer.parseInt( element.getAttribute("blue") ); • Color bgColor = new Color(r,g,b); Serializing using XML