Parsing XML DataKewang
Sample XML<?xml version="1.0" encoding="utf-8"?><CATALOG>  <CD>    <TITLE>Empire Burlesque</TITLE>    <ARTIST>Bob Dylan</A...
SAXSimple API for XML                     3
4
SAX sample (1/2)SAXParserFactory factory = SAXParserFactory.newInstance();SAXParser sp = factory.newSAXParser();XMLReader ...
SAX sample (2/2)private DefaultHandler handler = new DefaultHandler() {  private boolean hasTitle;  @Override  public void...
XML Pull sampleXmlPullParserFactory factory = XmlPullParserFactory.newInstance();XmlPullParser xpp = factory.newPullParser...
DOMDocument Object Model                        8
9
W3C DOM exampleDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.new...
JDOM exampleSAXBuilder sax = new SAXBuilder();Document doc = sax.build(new StringReader(xml.toString()));Element root = do...
Jsoup exampleDocument doc = Jsoup.parse(xml.toString());Elements titles = doc.select("TITLE");for (Element elem : titles) ...
Charts         13
XML parsing speed           JSOUP           JDOMMethod            W3C         XMLPULL            SAX                   0  ...
Code Line           JSOUP           JDOMMethod            W3C         XMLPULL            SAX                   0   10     ...
Which one?  SAX vs. DOM                16
Which one?      Memory Speed     Parser      Modify   TraversingSAX    Small   Fast     Event       Cant    One-wayDOM    ...
References●   Simple API for XML●   Document Object Model●   What is the "Thing" called XML?●   SAX & DOM parsers●    解析XM...
Upcoming SlideShare
Loading in …5
×

Parsing XML Data

1,771 views

Published on

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,771
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
40
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Parsing XML Data

  1. 1. Parsing XML DataKewang
  2. 2. 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
  3. 3. SAXSimple API for XML 3
  4. 4. 4
  5. 5. 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
  6. 6. 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 }};
  7. 7. 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}
  8. 8. DOMDocument Object Model 8
  9. 9. 9
  10. 10. 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}
  11. 11. 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
  12. 12. 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
  13. 13. Charts 13
  14. 14. XML parsing speed JSOUP JDOMMethod W3C XMLPULL SAX 0 100 200 300 400 500 600 700 14 Milliseconds
  15. 15. Code Line JSOUP JDOMMethod W3C XMLPULL SAX 0 10 20 30 40 50 60 15 Code Line
  16. 16. Which one? SAX vs. DOM 16
  17. 17. Which one? Memory Speed Parser Modify TraversingSAX Small Fast Event Cant One-wayDOM Large Slow Tree model Can Any-way 17
  18. 18. References● Simple API for XML● Document Object Model● What is the "Thing" called XML?● SAX & DOM parsers● 解析XML三种方式(PULL、SAX、DOM)● [分享] 處理簡單的 XML 文件 18

×