Title: Technologies for Grids and eBusiness XML 30'10'07
1Technologies for Grids and eBusinessXML30.10.
07
- Dr. Ramin YahyapourComputer Engineering
InstituteUniversity Dortmund
2XML Example
- lt?xml version"1.0"?gt
- ltbooklistgt
- ltbook available"yes"gt
- lttitlegt Java and XML lt/titlegt
- ltauthorgt Brett McLaughlin lt/authorgt
- ltisbngt 3-89721-280-3 lt/isbngt
- lt/bookgt
- ltbook available"yes"gt
- lttitlegt Java 2 Micro Edition lt/titlegt
- ltauthorgt Eric Giguere lt/authorgt
- ltisbngt 0-471-39065-8 lt/isbngt
- lt/bookgt
- ltbook available"no"gt
- lttitlegt Core Jini lt/titlegt
- ltauthorgt W. Keith Edwards lt/authorgt
- ltisbngt 3-8272-9592-0 lt/isbngt
- lt/bookgt
- lt/booklistgt
3SAX Parser (1)Import SAX Classes
- import org.xml.sax.Attributes
- import org.xml.sax.ContentHandler
- import org.xml.sax.Locator
- import org.xml.sax.SAXException
- import org.xml.sax.XMLReader
- import org.apache.xerces.parsers.SAXParser
4SAX Parser (2)Install Parser and Input Stream
- public class MySAXParser
- public void parseFile(String uri)
-
- public static void main(String args)
- MySAXParser mySAX new MySAXParser()
- mySAX.parseFile("booklist.xml")
-
5SAX Parser (3)Install ContentHandler and
Exceptions
- public void parseFile(String uri)
- XMLReader parser new SAXParser()
- // Create an instance of MyContentHandler
- ContentHandler myHandler new
MyContentHandler() - parser.setContentHandler(myHandler)
- try
- parser.parse(uri)
- catch (IOException e)
- System.out.println("Error reading
file " e.getMessage()) - System.exit(1)
- catch (SAXException e)
- System.out.println("Error parsing
file " e.getMessage()) - System.exit(1)
-
- System.out.println("File parsed
successfully!") -
6SAX Parser (4)ContentHandler
- class MyContentHandler implements ContentHandler
- private Locator locator
- public void setDocumentLocator(Locator
locator) - public void startDocument() throws
SAXException - public void endDocument() throws
SAXException - public void startPrefixMapping(String
prefix, String uri) - public void endPrefixMapping(String
prefix) - throws SAXException
-
7SAX Parser (5) ContentHandler
- class MyContentHandler implements ContentHandler
-
- public void startElement(
- String namespaceURI,
- String localName,
- String qName,
- Attributes atts)
- throws SAXException
- public void endElement(
- String namespaceURI,
- String localName,
- String qName)
- throws SAXException
-
-
8SAX Parser (6) ContentHandler
- class MyContentHandler implements ContentHandler
- public void characters(char ch, int
start, - int length)
- public void ignorableWhitespace(char
ch, - int start,
- int length)
- throws SAXException
- public void processingInstruction(String
target, - String data)
- throws SAXException
- public void skippedEntity(String name)
- throws SAXException
-
9ValidationExample DTD
- lt!ELEMENT Book ( Title,
- Author,
- ISBN)gt
- lt!ATTLIST Book
- available CDATA REQUIRED
- gt
- lt!ELEMENT Title (PCDATA)gt
- lt!ELEMENT Author (PCDATA)gt
- lt!ELEMENT ISBN (PCDATA)gt
10ValidationExample XML Schema
- lt?xml version"1.0"?gt
- ltschema targetNamespace"http//www.oreilly.com/ca
talog/javaxml/"gt - ltelement name"Book" type"BookType" /gt
- ltcomplexType name"BookType"gt
- ltelement nameTitle" type"string" /gt
- ltelement nameAuthor" typestring" /gt
- ltelement nameISBN" type"string" /gt
- ltattribute nameavailable" defaultyes"gt
- ltsimpleType base"string"gtlt/simpleTypegt
- lt/attributegt
- lt/complexTypegt
- lt/schemagt
11XML Document Tree
Book
XHTML
Price
ISBN
Content
HEAD
BODY
Title
Author
Chapter
Title
Chapter
Transforming Document Trees
12Transformations with XSL
- lt?xml version"1.0"?gt
- ltxslstylesheet version"1.0"
xmlnsxsl"http//www.w3.org/1999/XSL/Transform" - gt
- Rules
- lt/xslstylesheetgt
13Transformations with XSLRules
- lt?xml version"1.0"?gt
- ltxslstylesheet version"1.0"
xmlnsxsl"http//www.w3.org/1999/XSL/Transform" - gt
- ltxsltemplate matchBook"gt
- Action
- lt/xsltemplategt
- lt/xslstylesheetgt
14Transformations with XSLExample of Transformation
- lt?xml version"1.0"?gt
- ltxslstylesheet version"1.0"
xmlnsxsl"http//www.w3.org/1999/XSL/Transform" - gt
- ltxsltemplate matchBook"gt
- lthtmlgt
- ltheadgt
- lttitlegt ltxslvalue-of select"title"/gt
lt/titlegt - lt/headgt
- ltbodygt
- ltxslapply-templates select" /gt
- lt/bodygt
- lt/htmlgt
- lt/xsltemplategt
- lt/xslstylesheetgt
XPath-Referencing
XPath-Filtering
15Transformations with XSLIterations
- ltxsltemplate matchBook"gt
- ltxslfor-each selectChapter"gt
- ltligt
- ltxslvalue-of selectChapterName" /gt
- lt/ligt
- lt/xslfor-eachgt
- lt/xsltemplategt
16Using DOM
- public class DOMParserDemo
- public void performDemo(String uri)
- System.out.println("Parsing XML File "
uri "\n\n") - // Instantiate your vendor's DOM parser
implementation - DOMParser parser new DOMParser()
- parser.setFeature("http//xml.org/sax/feat
ures/validation", true) - parser.parse(uri)
- Document doc parser.getDocument()
- do something with DOM
-
-
- public static void main(String args)
17Using DOM (2)Example for Accessing DOM Nodes
- public void printNode(Node node, String indent)
- switch (node.getNodeType())
- case Node.DOCUMENT_NODE
- System.out.println("ltxml
version\"1.0\"gt\n") - // recurse on each child
- NodeList nodes
node.getChildNodes() - if (nodes ! null)
- for (int i0
iltnodes.getLength() i) - printNode(nodes.item(i),
"") -
-
- break
- case Node.ELEMENT_NODE
- case Node.TEXT_NODE
- case Node.CDATA_SECTION_NODE
- case Node.PROCESSING_INSTRUCTION_NODE
- case Node.ENTITY_REFERENCE_NODE
- case Node.DOCUMENT_TYPE_NODE
18Using DOM (2)Example for Accessing DOM Nodes
- public void printNode(Node node, String indent)
- switch (node.getNodeType())
- case Node.DOCUMENT_NODE
- case Node.ELEMENT_NODE
- String name node.getNodeName()
- System.out.print(indent "lt"
name) - NamedNodeMap attributes
node.getAttributes() - for (int i0 iltattributes.getLeng
th() i) - Node current
attributes.item(i) - System.out.print(" "
current.getNodeName() - "\""
current.getNodeValue() - "\"")
-
- System.out.println("gt")
- // recurse on each child
- NodeList children
node.getChildNodes() - if (children ! null)
- for (int i0
iltchildren.getLength() i)