Parsing XML Data
Upcoming SlideShare
Loading in...5
×
 

Parsing XML Data

on

  • 1,441 views

 

Statistics

Views

Total Views
1,441
Views on SlideShare
1,441
Embed Views
0

Actions

Likes
0
Downloads
33
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Parsing XML Data Parsing XML Data Presentation Transcript

  • Parsing XML DataKewang
  • Sample XML<?xml version="1.0" encoding="utf-8"?><CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tyler</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS Records</COMPANY> <PRICE>9.90</PRICE> <YEAR>1988</YEAR> </CD></CATALOG> 2
  • SAXSimple API for XML 3
  • 4
  • SAX sample (1/2)SAXParserFactory factory = SAXParserFactory.newInstance();SAXParser sp = factory.newSAXParser();XMLReader xr = sp.getXMLReader();InputSource is = new InputSource(new StringReader(xml.toString()));xr.setContentHandler(handler);xr.setErrorHandler(handler);xr.parse(is); 5
  • SAX sample (2/2)private DefaultHandler handler = new DefaultHandler() { private boolean hasTitle; @Override public void characters(char[] ch, int start, int length) { if (hasTitle) { txtResult.setText(new String(ch, start, length)); } } @Override public void endElement(String uri, String lName, String qName) { hasTitle = false; } @Override public void startElement(String uri, String lName, String qName,Attributes attrs) { hasTitle = lName.equals("TITLE"); 6 }};
  • XML Pull sampleXmlPullParserFactory factory = XmlPullParserFactory.newInstance();XmlPullParser xpp = factory.newPullParser();xpp.setInput(new StringReader(xml.toString()));int eventType = xpp.getEventType();while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_TAG: if (xpp.getName().equals("TITLE")) { txtResult.setText(xpp.nextText()); } break; } eventType = xpp.next(); 7}
  • DOMDocument Object Model 8
  • 9
  • W3C DOM exampleDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document doc = builder.parse(newByteArrayInputStream(xml.toString().getBytes()));Element root = doc.getDocumentElement();NodeList cds = root.getChildNodes();for (int i = 0; i < cds.getLength(); i++) { Node cd = cds.item(i); if (cd.getNodeType() == Node.ELEMENT_NODE) { NodeList titles = cd.getChildNodes(); for (int j = 0; j < titles.getLength(); j++) { Node title = titles.item(j); if (title.getNodeType() == Node.ELEMENT_NODE &&title.getNodeName().equals("TITLE")) { txtResult.setText(title.getFirstChild().getNodeValue()); } } } 10}
  • JDOM exampleSAXBuilder sax = new SAXBuilder();Document doc = sax.build(new StringReader(xml.toString()));Element root = doc.getRootElement();for (Element elem : root.getChildren("CD")) { txtResult.setText(elem.getChildText("TITLE"));} inner Structure: SAX parser outer Structure: DOM operation 11
  • Jsoup exampleDocument doc = Jsoup.parse(xml.toString());Elements titles = doc.select("TITLE");for (Element elem : titles) { txtResult.setText(elem.text());} v1.6.2(2012/3/27): add XML parser support selector syntax 12
  • Charts 13
  • XML parsing speed JSOUP JDOMMethod W3C XMLPULL SAX 0 100 200 300 400 500 600 700 14 Milliseconds
  • Code Line JSOUP JDOMMethod W3C XMLPULL SAX 0 10 20 30 40 50 60 15 Code Line
  • Which one? SAX vs. DOM 16
  • Which one? Memory Speed Parser Modify TraversingSAX Small Fast Event Cant One-wayDOM Large Slow Tree model Can Any-way 17
  • References● Simple API for XML● Document Object Model● What is the "Thing" called XML?● SAX & DOM parsers● 解析XML三种方式(PULL、SAX、DOM)● [分享] 處理簡單的 XML 文件 18