|
Distributed Computing ![]() This website demonstrates using wikis as teaching and learning tool. The course instructor is also happy to share the teaching materials here with those who find it readable. |
Lecture /
Java and XMLA Distributed Computing Lecture by Steven Choy
XML Basics and OverviewExtensible Markup Language
<?xml version="1.0" standalone="yes" ?> <customers> <customer> <customerno>1</customerno> <first>Peter</first> <last>Chan</last> <telephone>12345678</telephone> </customer> <customer> <customerno>2</customerno> <first>David</first> <last>Lau</last> <telephone>87654321</telephone> </customer> </customers> Well-formed XML document
Valid XML document
XML DTD and XML Schema
More XML Example
XML with internal DTD<?xml version="1.0" standalone="yes"?> <!DOCTYPE customer [ <!ELEMENT customer (first, last)> <!ELEMENT first (#PCDATA)> <!ELEMENT last (#PCDATA)> ]> <customer> <first>Peter</first> <last>Chan</last> </customer> XML with external DTD<?xml version="1.0" standalone="yes"?> <!DOCTYPE customer SYSTEM “customer_dtd2.dtd”> <customer> <first>Peter</first> <last>Chan</last> </customer> <!ELEMENT customer (first, last)> <!ELEMENT first (#PCDATA)> <!ELEMENT last (#PCDATA)> XML with XSD<?xml version="1.0"?> <customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="customer_xsd1.xsd"> <first>Peter</first> <last>Chan</last> </customer>
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="customer"> <xs:complexType> <xs:sequence> <xs:element name="first" type="xs:string"/> <xs:element name="last" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> Why DTD?
More about XSD
XML Scheme or DTD?
The Document Type Definition (DTD) defines the valid syntax of a class of XML documents. (The Document Type Definition (DTD) is the method used to define all markup languages. The purpose of DTD is to define the legal building blocks of an XML document.)
A schema is used to describe the possible data content of a document in a very rigorous and formal way. (XML Schema language (often called XSD) is used to describe both the structure and the content of an XML document.)
The limitations of a DTD: DTD does not have XML syntax and offers only limited support for types or namespaces. DTDs call for elements to consist of one of three things: (1) A text string; (2) A text string with other child elements mixed together; (3) A set of child elements.
How to validate a XML document
Do you really know what is XML?
W3Schools XML Quiz - 20 Quiz
Other similar names related to XML
Processing XML with JavaSAX, DOM, and JAXP
SAX (Simple API for XML)"At its core, SAX, the Simple API for XML, is based on just two interfaces, the XMLReader interface that represents the parser and the ContentHandler interface that receives data from the parser. These two interfaces alone suffice for 90% of what you need to do with SAX."
Reference: XMLReader API from java.sun.com
Reference: ContentHandler API from java.sun.com
Reference: Processing XML with Java
Reference: SAX Project
SAX Basics
The SAX ContentHandler interfacepackage org.xml.sax; public interface ContentHandler { public void setDocumentLocator(Locator locator); public void startDocument() throws SAXException; public void endDocument() throws SAXException; public void startPrefixMapping(String prefix, String uri) throws SAXException; public void endPrefixMapping(String prefix) throws SAXException; public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes atts) throws SAXException; public void endElement(String namespaceURI, String localName, String qualifiedName) throws SAXException; public void characters(char[] text, int start, int length) throws SAXException; public void ignorableWhitespace(char[] text, int start, int length) throws SAXException; public void processingInstruction(String target, String data) throws SAXException; public void skippedEntity(String name) throws SAXException; } Example: A SAX event handler
/* ParserSax1.java */ import org.xml.sax.*; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParser; public class ParserSax1 extends DefaultHandler { public static void main(String argv[]) { // Use itself as the event handler DefaultHandler handler = new ParserSax1(); // create the parser object SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); // enable validation! try { SAXParser saxParser = factory.newSAXParser(); saxParser.parse(argv[0], handler); } catch (Exception e) { System.out.println(e); } System.exit(0); } // Event Handler methods public void startDocument() { System.out.println("Starting to read document"); } public void endDocument() { System.out.println("Finish reading document"); } public void startElement(String namespaceURI, String lName, // local name String qName, // qualified name Attributes attrs) { if ((qName.equals("first")) || (qName.equals("last"))) System.out.print(qName + " begins here >>>"); } public void endElement(String namespaceURI, String lName, String qName) { if ((qName.equals("first")) || (qName.equals("last"))) System.out.println("<<< " + qName + " ends here."); } public void characters(char buf[], int offset, int len) { String s = new String(buf, offset, len); System.out.print(s); } } The Document Object Model
XML DOM Basics
/* DOMParser.java */ import javax.xml.parsers.*; import org.w3c.dom.*; class DOMParser { public static void main(String[] argv) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(argv[0]); printTree(doc); } catch (Exception e) { System.out.println(e); } } static void printTree(Document d) { Node rootNode = d.getDocumentElement(); System.out.println("Root node : " + rootNode.getNodeName()); printChildNodes(rootNode); } static void printChildNodes(Node n) { Node thisNode; NodeList theList = n.getChildNodes(); for (int i=0; i < theList.getLength(); i++) { thisNode = theList.item(i); System.out.println(thisNode.getNodeName()); printChildNodes(thisNode); } } } SAX and DOM APIs"The SAX API is event-based. XML parsers that implement the SAX API generate events that correspond to different features found in the parsed XML document. By responding to this stream of SAX events in Java code, you can write programs driven by XML-based data."
"The DOM API is an object-model-based API. XML parsers that implement DOM create a generic object model in memory that represents the contents of the XML document. Once the XML parser has completed parsing, the memory contains a tree of DOM objects that offers information about both the structure and contents of the XML document."
The Java API for XML Processing (JAXP)
Extra Materials for Probing FurtherLearn more about XML
Learn more about processing XML with Java
XML Editorsa complete cross platform XML editor providing the tools for XML authoring, XML conversion, XML Schema, DTD, Relax NG and Schematron development, XPath, XSLT, XQuery debugging, SOAP and WSDL testing
allows to edit large, complex, modular, XML documents. It makes it easy mastering XML vocabularies such as DocBook or DITA.
The "visual" part comes from the fact that Vex hides the raw XML tags from the user, providing instead a wordprocessor-like interface. Because of this, Vex is best suited for "document-style" XML documents such as XHTML and DocBook rather than "data-style" XML documents.
XMLSpy - XML editor for modeling, editing, transforming, and debugging XML technologies
It is a free and Windows-based XML editor and development environment for XML, DTD, and XSLT documents
XML Copy Editor is a fast, free, validating XML editor. It has both Windows and Linux versions.
Thanks for ReadingIf you would rather like to have this lecture note in printed format, please click the print action link in the top right corner. If you find any problem in this lecture note, please feel free to reach Steven by steven@findaway.hk |