xml programming xml programming
play

XML Programming XML Programming documents Anders Mller & - PowerPoint PPT Presentation

Objectives Objectives How XML may be manipulated from general- purpose programming languages An Introduction to XML and Web Technologies An Introduction to XML and Web Technologies How streaming may be useful for handling large XML


  1. Objectives Objectives � How XML may be manipulated from general- purpose programming languages An Introduction to XML and Web Technologies An Introduction to XML and Web Technologies � How streaming may be useful for handling large XML Programming XML Programming documents Anders Møller & Michael I. Schwartzbach  2006 Addison-Wesley An Introduction to XML and Web Technologies 2 General Purpose XML Programming The JDOM Framework General Purpose XML Programming The JDOM Framework � Needed for: � An implementation of generic XML trees in Java • domain-specific applications � Nodes are represented as classes and interfaces • implementing new generic tools � DOM is a language-independent alternative � Important constituents: • parsing XML documents into XML trees • navigating through XML trees • manipulating XML trees • serializing XML trees as XML documents An Introduction to XML and Web Technologies 3 An Introduction to XML and Web Technologies 4 1

  2. JDOM Classes and Interfaces A Simple Example JDOM Classes and Interfaces A Simple Example � The abstract class Content has subclasses: int xmlHeight(Elem lemen ent e) { java.util.List contents = e.get getCo Conte ntent nt(); • Comment java.util.Iterator i = contents.iterator(); int max = 0; • DocType while (i.hasNext()) { • Element Object c = i.next(); int h; • EntityRef if (c instanceof Elemen Element) • ProcessingInstruction h = xmlHeight((Elemen Element)c); else • Text h = 1; � Other classes are Attribute and Document if (h > max) max = h; � The Parent interface describes Document and } Element return max+1; } An Introduction to XML and Web Technologies 5 An Introduction to XML and Web Technologies 6 Another Example A Final Example (1/3) Another Example A Final Example (1/3) static void doubleSugar(Docume Document nt d) � Modify all elements like throws DataConversionException { Namesp Namespac ace rcp = <ingredient name="butter" amount="0.25" unit="cup"/> Namesp Namespac ace.g e.getN etNam amesp espac ace("http://www.brics.dk/ixwt/recipes"); into a more elaborate version: Filter Filter f = new Elemen ElementF tFil ilter ter("ingredient",rcp); java.util.Iterator i = d.getDes getDesce cenda ndant nts(f); <ingredient name="butter"> while (i.hasNext()) { <ingredient name="cream" unit="cup" amount="0.5" /> Element e = (Elemen Elemen Element)i.next(); <preparation> if (e.getAt getAttri tribu buteV teVal alue ue("name").equals("sugar")) { Churn until the cream turns to butter. double amount = e.getAtt getAttrib ribut ute("amount").getDoubleValue(); </preparation> e.setA setAttr ttribu ibute te("amount",new Double(2*amount).toString()); </ingredient> } } } An Introduction to XML and Web Technologies 7 An Introduction to XML and Web Technologies 8 2

  3. A Final Example (2/3) A Final Example (3/3) A Final Example (2/3) A Final Example (3/3) void makeButter(Elem Element ent e) throws DataConversionException { Element cream = new Element Elemen Element("ingredient",rcp); Namesp Namespac ace rcp = cream.setAtt setAttri ribut bute("name","cream"); Namespac Namesp ace.g e.getN etNam amesp espac ace("http://www.brics.dk/ixwt/recipes"); cream.setAtt setAttri ribut bute("unit",c.getAtt getAttri ribut buteVa eValu lue("unit")); java.util.ListIterator i = e.getC getChil hildr dren en().listIterator(); double amount = c.getAtt etAttrib ribut ute("amount").getDoubleValue(); while (i.hasNext()) { cream.setAtt setAttri ribut bute("amount",new Double(2*amount).toString()); Elemen Element c = (Elemen Element)i.next(); butter.ad addCo dCont ntent ent(cream); if (c.getNa getName me().equals("ingredient") && Element churn = new Element Elemen Element("preparation",rcp); c.getAt getAttri tribu buteV teVal alue ue("name").equals("butter")) { churn.addCon addConte tent nt("Churn until the cream turns to butter."); Elemen Element butter = new Elemen Element("ingredient",rcp); butter.ad addCo dCont ntent ent(churn); butter.se setAt tAttr tribu ibute te("name","butter"); i.set((El Eleme ement nt)butter); } else { makeButter(c); } } } An Introduction to XML and Web Technologies 9 An Introduction to XML and Web Technologies 10 Parsing and Serializing Validation (DTD) Parsing and Serializing Validation (DTD) public class ValidateDTD { public class ChangeDescription { public static void main(String[] args) { public static void main(String[] args) { try { try { SAXBuilder b = new SAXBuilder(); SAXBuilde SAXBui lder b = new SAXBu SAXBuild ilder er(); b.setV setVali alidat datio ion(true); Docume Document nt d = b.build build(new File("recipes.xml")); String msg = "No errors!"; Namespace Namesp ace rcp = try { Name Namespa space. ce.ge getNa tName mesp space ace("http://www.brics.dk/ixwt/recipes"); Document d = b.build(new File(args[0])); d.getR getRoot ootEle Eleme ment nt().getCh getChild ild("description",rcp) } catch (JDO JDOMP MPars arseE eExc xcept eption ion e ) { .setTe setText xt("Cool recipes!"); msg = e.getMessage(); XMLOutput XMLOut putter ter outputter = new XMLOu XMLOutp tputt utter er(); } outputter.outp output ut(d,System.out); System.out.println(msg); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } } } } } An Introduction to XML and Web Technologies 11 An Introduction to XML and Web Technologies 12 3

  4. Validation (XML Schema) XPath Evaluation Validation (XML Schema) XPath Evaluation public class ValidateXMLSchema { public static void main(String[] args) { void doubleSugar(Document d) throws JDOMException { try { XPath XPath p = XPath. XPath.ne newIn wInst stan ance ce("//rcp:ingredient[@name='sugar']"); SAXBuilder b = new SAXBuilder(); p.addNam addNames espac pace("rcp","http://www.brics.dk/ixwt/recipes"); java.util.Iterator i = p.select selectNo Nodes des(d).iterator(); b.setV setVali alidat datio ion(true); b.setP setProp ropert erty( while (i.hasNext()) { "http://java.sun.com/xml/jaxp/properties/schemaLanguage", Element e = (Element)i.next(); double amount = e.getAttribute("amount").getDoubleValue(); "http://www.w3.org/2001/XMLSchema"); String msg = "No errors!"; e.setAttribute("amount",new Double(2*amount).toString()); try { } Document d = b.build(new File(args[0])); } } catch (JDO JDOMP MPars arseE eExc xcept eption ion e ) { msg = e.getMessage(); } System.out.println(msg); } catch (Exception e) { e.printStackTrace(); } } } An Introduction to XML and Web Technologies 13 An Introduction to XML and Web Technologies 14 XSLT Transformation Business Cards XSLT Transformation Business Cards <cardlist xmlns="http://businesscard.org" xmlns:xhtml="http://www.w3.org/1999/xhtml"> public class ApplyXSLT { <title> public static void main(String[] args) { <xhtml:h1>My Collection of Business Cards</xhtml:h1> try { containing people from <xhtml:em>Widget Inc.</xhtml:em> SAXBuilder b = new SAXBuilder(); </title> Document d = b.build(new File(args[0])); <card> XSLTransformer XSLTrans former t = new XSLTransforme XSLTransformer(args[1]); <name>John Doe</name> Document h = t.transfo transform rm(d); <title>CEO, Widget Inc.</title> <email>john.doe@widget.com</email> XMLOutputter outputter = new XMLOutputter(); <phone>(202) 555-1414</phone> outputter.output(h,System.out); </card> } catch (Exception e) { e.printStackTrace(); } <card> } <name>Joe Smith</name> } <title>Assistant</title> <email>thrall@widget.com</email> </card> </cardlist> An Introduction to XML and Web Technologies 15 An Introduction to XML and Web Technologies 16 4

Recommend


More recommend