Your SlideShare is downloading. ×
Parsing XML Data
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Parsing XML Data

1,429
views

Published on

Published in: Technology, Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,429
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
39
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×