Download presentation
Presentation is loading. Please wait.
Published byCory Harrington Modified over 9 years ago
1
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools
2
CSE 6331 © Leonidas Fegaras XML Tools2 XML Processing document parser document validator application XML document XML infoset XML infoset (annotated) Well-formedness checks Reference expansion DTD or XML schema storage system
3
CSE 6331 © Leonidas Fegaras XML Tools3 DOM The Document Object Model (DOM) is a platform- and language-neutral interface that allows programs and scripts to dynamically access and update the content and structure of XML documents. The following is part of the DOM interface: public interface Node { public String getNodeName (); public String getNodeValue (); public NodeList getChildNodes (); public NamedNodeMap getAttributes ();... } public interface Element extends Node { public Node getElementsByTagName ( String name );... } public interface Document extends Node { public Element getDocumentElement ();... } public interface NodeList { public Node item ( int index ); public int getLength (); public Node item ( int index ); }
4
CSE 6331 © Leonidas Fegaras XML Tools4 DOM Example import java.io.File; import javax.xml.parsers.*; import org.w3c.dom.*; class Test { public static void main ( String args[] ) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new File("depts.xml")); NodeList nodes = doc.getDocumentElement().getChildNodes(); for (int i=0; i<nodes.getLength(); i++) { Node n = nodes.item(i); NodeList ndl = n.getChildNodes(); for (int k=0; k<ndl.getLength(); k++) { Node m = ndl.item(k); if ( (m.getNodeName() == "dept") && (m.getFirstChild().getNodeValue() == "cse") ) { NodeList ncl = ((Element) m).getElementsByTagName("tel"); for (int j=0; j<ncl.getLength(); j++) { Node nc = ncl.item(j); System.out.print(nc.getFirstChild().getNodeValue()); } } }
5
CSE 6331 © Leonidas Fegaras XML Tools5 SAX SAX is the Simple API for XML that allows you to process a document as it's being read (in contrast to DOM, which requires the entire document to be read before it takes any action). The SAX API is event based. The XML parser sends events, such as the start or the end of an element, to an event handler, which processes the information. Parser events: void startDocument () Receive notification of the beginning of a document. void endDocument () Receive notification of the end of a document. void startElement ( String namespace, String localName, String qName, Attributes atts ) Receive notification of the beginning of an element. void endElement ( String namespace, String localName, String qName ) Receive notification of the end of an element. void characters ( char[] ch, int start, int length ) Receive notification of character data.
6
CSE 6331 © Leonidas Fegaras XML Tools6 SAX Example import java.io.FileReader; import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; class SimpleHandler extends DefaultHandler { public static void main ( String args[] ) throws Exception { SAXParserFactory pf = SAXParserFactory.newInstance(); SAXParser parser = pf.newSAXParser(); XMLReader reader = parser.getXMLReader(); SimpleHandler handler = new SimpleHandler(); parser.parse(new InputSource(new FileReader("a.xml")), handler); } public SimpleHandler () { super(); }
7
CSE 6331 © Leonidas Fegaras XML Tools7 SAX Example (cont.) public void startDocument () { System.out.println("Start document"); } public void endDocument () { System.out.println("End document"); } public void startElement ( String uri, String name, String tag, Attributes atts ) { System.out.println("Start element: " + tag); } public void endElement ( String uri, String name, String tag ) { System.out.println("End element: " + tag); } public void characters ( char text[], int start, int length ) { System.out.print("Characters: \""); for (int i = start; i < start + length; i++) System.out.print(text[i]); System.out.print("\"\n"); }
8
CSE 6331 © Leonidas Fegaras XML Tools8 XPath in SAX /department/gradstudent[name/lastname=“Smith”]/address Need to construct the finite state machine: –Initial state: 2 –Print state: 1 (the element is part of the solution) –Skip state: 0 (the element is not part of the solution) –Store state: -1 (when we don’t know if the element is part of the solution or not) Need –s: a stack of states –result: a place to hold the stored elements when state=-1 –count: to count the nesting level in an element –pred1: one for each predicate; values: 0 (false), 1 (true), -1 (unknown) 2 3456 department gradstudent name lastname address
9
CSE 6331 © Leonidas Fegaras XML Tools9 /department/gradstudent[name/lastname=“Smith”]/address public void startElement ( String u, String name, String tag, Attributes a ) { if (s.top()==2 && tag.equals("department")) s.push(3); else if (s.top()==3 && tag.equals("gradstudent")) { s.push(4); pred1 = -1; result = ""; } else if (s.top()==4 && tag.equals("name")) s.push(5); else if (s.top()==4 && tag.equals("address")) { count = 1; if (pred1==1) { s.push(1); System.out.print(" "); } else if (pred1==0) s.push(0); else { s.push(-1); result = result + " "; } } else if (s.top()==5 && tag.equals("lastname")) s.push(6); else if (s.top()==1) { count++; System.out.print(" "); } else if (s.top()==0) count++; else if (s.top()==-1) { count++; result = result + " "; } else if (s.top()>0) { s.push(0); count = 1; } } 2 3456 department gradstudent name lastname address
10
CSE 6331 © Leonidas Fegaras XML Tools10 /department/gradstudent[name/lastname=“Smith”]/address public void endElement ( String uri, String name, String tag ) { if (s.top()<=1) { if (s.top()==1) System.out.print(" "); if (s.top()==-1) result = result + " "; if (--count==0) s.pop(); } else s.pop(); } public void characters ( char text[], int start, int length ) { if (s.top()==6) if ((new String(text,start,length)).equals(“Smith")) { pred1 = 1; if (!result.equals("")) System.out.print(result); } else pred1 = 0; else if (s.top()==1) System.out.print(new String(text,start,length)); else if (s.top()==-1) result = result + new String(text,start,length); } } 2 3456 department gradstudent name lastname address
11
CSE 6331 © Leonidas Fegaras XML Tools11 XSL Transformation A stylesheet specification language for converting XML documents into various forms (XML, HTML, plain text, etc). Can transform each XML element into another element, add new elements into the output file, or remove elements. Can rearrange and sort elements, test and make decisions about which elements to display, and much more. Based on XPath: <xsl:stylesheet version=’1.0’ xmlns:xsl=’http//www.w3.org/1999/XSL/Transform’>
12
CSE 6331 © Leonidas Fegaras XML Tools12 XSLT Templates XSL uses XPath to define parts of the source document that match one or more predefined templates. When a match is found, XSLT will transform the matching part of the source document into the result document. The parts of the source document that do not match a template will end up unmodified in the result document. Form: … The default (implicit) templates visit all nodes and strip out all tags:
13
CSE 6331 © Leonidas Fegaras XML Tools13 Other XSLT Elements select the value of an XML element and add it to the output stream of the transformation, e.g.. copy the entire XML element to the output stream of the transformation. apply the template rules to the elements that match the XPath expression. … add an element to the output with a tag-name derived from the XPath. Example: <xsl:stylesheet version = ’1.0’ xmlns:xsl=’http://www.w3.org/1999/XSL/Transform’>
14
CSE 6331 © Leonidas Fegaras XML Tools14 Copy the Entire Document <xsl:stylesheet version = ’1.0’ xmlns:xsl=’http://www.w3.org/1999/XSL/Transform’>
15
CSE 6331 © Leonidas Fegaras XML Tools15 More on XSLT Conflict resolution: more specific templates overwrite more general templates. Templates are assigned default priorities, but they can be overwritten using priority=“n” in a template. Modes can be used to group together templates. No mode is an empty mode. Conditional and loop statements: body Variables can be used to name data: value Variables are used as {$x} in XPaths.
16
CSE 6331 © Leonidas Fegaras XML Tools16 Using XSLT import java.io.FileReader; import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import javax.xml.transform.*; import javax.xml.transform.sax.*; import javax.xml.transform.stream.*; class Test { public static void main ( String args[] ) throws Exception { TransformerFactory tfactory = TransformerFactory.newInstance(); SAXParserFactory pfactory = SAXParserFactory.newInstance(); SAXParser parser = pfactory.newSAXParser(); XMLReader reader = parser.getXMLReader(); SimpleHandler handler = new SimpleHandler(); Transformer t = tfactory.newTransformer(new StreamSource("x.xsl")); SAXSource source = new SAXSource(reader,new InputSource("a.xml")); t.transform(source,new StreamResult("a.html")); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.