1 Software Engineering 1 XML 1.1 XML Extensible Markup Language Auszeichnungssprache zur Darstellung hierarchisch strukturierter Daten in Form von Textdaten Wird für den Datenaustausch/Speicherung benutzt Spezifiziert durch das W3C Strukturelle oder Inhaltliche Einschränkungen können mit DTD oder einem XML Schema definiert werden 1.2 XML - Aufbau <?xml version="1.0" encoding="utf-8"?> <contact xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/x <creation-date>2008-03-12t16:36:45</creation-date> <name> <full-name>arthur Zaczek</full-name> <given-name>arthur</given-name> <last-name>zaczek</last-name> </name> <birthday>1978-03-20t00:00:00</birthday> <address> <type>business</type> <street>...</street> <locality>klosterneuburg</locality> <postal-code>3400</postal-code> <country>österreich</country> </address> <uid>ko-ct-89c337bc-1c5f-4e36-b8b0-0c8c781c5d65</uid> <phone> <type>home1</type> <number>+43 (664) 123456789</number> </phone> <email> <display-name>arthur Zaczek</display-name> <smtp-address>zaczek@technikum-wien.at</smtp-address> </email> </contact> 1.3 XML Elemente Arthur Zaczek Attribute +43 (664) 123456789 1
Document Element...... XML Deklaration 1.4 Namespaces Tags können Namespaces zugeordnet werden Somit können mehrere Gruppen von Daten in einem Dokument untergebracht werden, ohne dass sich Tagnamen überschneiden ~ ~ ~~ ~~ {.cs Arthur Zaczek Arthur Zaczek zaczek@technikum-wien.at ~ ~ ~~ ~~ 1.5 DOM Document Object Model Spezifikation einer Programmierschnittstelle Wird vom W3C definiert XML Dokument wird als Objektbaum dargestellt Wird somit vollständig ins RAM geladen Ist nicht für große Dokumente geeignet! 1.6 DOM - Aufbau 1.7 DOM - Node appendchild getattributes getchildnodes getfirst/lastchild getlocalname getnamespaceuri getnodevalue getnodetype getparentnode... 1.8 DOM lesen/java DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newdocumentbuilder(); Document xml = db.parse(xmlinput); Element root = xml.getdocumentelement(); String description = Utils.getXmlElementString(root, "body"); String title = Utils.getXmlElementString(root, "summary"); Time start = Utils.getXmlElementTime(root, "start-date"); Time end = Utils.getXmlElementTime(root, "end-date"); 2
1.9 DOM lesen/java public static final String getxmlelementstring(element parent, String name) { Element e = getxmlelement(parent, name); return getxmlelementstring(e); public static final String getxmlelementstring(element e) { if (e == null) return null; NodeList nl = e.getchildnodes(); if (nl.getlength() > 0) { return nl.item(0).getnodevalue(); return null; 1.10 DOM lesen/java public static final int getxmlelementint(element parent, String name, int defaultvalue) { String value = getxmlelementstring(parent, name); if (value == null "".equals(value)) return defaultvalue; try { return Integer.parseInt(value); catch (TimeFormatException tfe) { Log.e("sync", "Unable to parse DateTime " + value); return defaultvalue; 1.11 DOM neues Dokument public final static Document newdocument(string rootname) throws ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newdocumentbuilder(); Document xml = db.newdocument(); Node root = xml.createelement(rootname); Attr a = xml.createattribute("version"); a.setvalue("1.0"); root.getattributes().setnameditem(a); xml.appendchild(root); return xml; 1.12 XPath Ist eine Abfragesprache Adressiert teile eines XML Baumes Vom W3C entwickelt //phone[type= home ] Liefert alle phone Elemente (auf allen Ebenen) mit dem Attribut type=home 3
1.13 XPath - Java DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(new File("/widgets.xml")); XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "/widgets/widget"; Node widgetnode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE); 1.14 SAX Parser Liest XML-Daten als sequentiellen Datenstrom Im Gegensatz zu DOM, der XML als Objektbaum abbildet Der Speicherbedarf wird minimiert 1.15 SAX Parser C XmlTextReader GetAttribute() MoveTo*() Read() ReadAttributeValue() ReadContentAs*() ReadSubTree() Name LocalName Value 1.16 Aufbau eines SAX Parsers (C#) Jedes Element, welches Unterelement hat wird von einer eigenen Methode geparst 1.17 Aufbau eines SAX Parsers (C#) void ReadOsm(System.Xml.XmlTextReader xml) void ReadAnyOsmElement(System.Xml.XmlReader osm) void ReadTag(System.Xml.XmlReader element, Address a) 1.18 Aufbau public static void Update() { var file = "austria.osm"; using (var fs = File.OpenRead(file)) using (var xml = new System.Xml.XmlTextReader(fs)) { while (xml.read()) { if (xml.nodetype == System.Xml.XmlNodeType.Element && xml.name == "osm") { 4
ReadOsm(xml); 1.19 Aufbau private static void ReadOsm(System.Xml.XmlTextReader xml) { using (var osm = xml.readsubtree()) { while (osm.read()) { if (osm.nodetype == System.Xml.XmlNodeType.Element && (osm.name == "node" osm.name == "way")) { ReadAnyOsmElement(osm); 1.20 Aufbau private static void ReadAnyOsmElement(System.Xml.XmlReader osm) { using (var element = osm.readsubtree()) { while (element.read()) { if (element.nodetype == System.Xml.XmlNodeType.Element && element.name == "tag") { ReadTag(element, a); 1.21 Aufbau private static void ReadTag(System.Xml.XmlReader element, Address a) { string tagtype = element.getattribute("k"); string value = element.getattribute("v"); switch (tagtype) { case "addr:city": a.city = value; case "addr:postcode": a.postcode = value; case "addr:street": a.street = value; 5