XML Android Club 2015. Agenda XML JSON XML eXtensible Markup Language Used for: data transfer PHP ->...
-
Upload
rudolph-stevenson -
Category
Documents
-
view
226 -
download
0
Transcript of XML Android Club 2015. Agenda XML JSON XML eXtensible Markup Language Used for: data transfer PHP ->...
XML
Android Club 2015
Agenda
• XML• JSON
XML
• eXtensible Markup Language• Used for: data transfer• PHP -> Java
XML: example
XML: example
<?xml version="1.0" encoding="UTF-8" ?><countries> <country> <name>Uzbekistan</name> <capital>Tashkent</capital> <currency>Sum</ currency> </country></countries>
XML: practice
• Add following countries:• Russia – Moscow – Ruble• China – Beijing – Yuan• Japan – Tokyo – Yen• US – Washington – US Dollar• Germany – Berlin - Euro
XML Parsing (разобрать)
• DOM• JDOM• StAX• JAXB• Lynda.com - XML Integration with Java
Android XML parsers
• DOM• SAX• XmlPullParser
DOM: example
• DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
• DocumentBuilder builder = factory.newDocumentBuilder();
• Document doc = builder.parse("http://www.w3schools.com/xml/simple.xml");
DOM: example
• NodeList list = doc.getElementsByTagName("food");
• for (int i = 0; i < list.getLength(); i++) {Element element = (Element) list.item(i);String value =
element.getElementsByTagName("name").item(0).getTextContent();
System.out.println(value);• }
DOM: practice
• http://joerichard.net/api/ac/countries.xml
• Create DocumentBuilderFactory• Create DocumentBuilder • Create Document -> use before
stated URL• Get node list• Print country names
DOM: practice (cont)
• http://joerichard.net/api/ac/countries.xml
• Print: [CAPITAL] is capital city of [COUNTRY]
• Example: Tashkent is capital city of Uzbekistan
DOM: practice 2
• http://joerichard.net/api/ac/books.xml• Create DocumentBuilderFactory• Create DocumentBuilder• Create Document -> use before
stated URL• Get node list• Print book titles
POJO
• Plain old java object
XML - example
<food><name>Belgian Waffles</name>
<price>$5.95</price><description>Two of our famous
Belgian Waffles with plenty of real maple syrup
</description><calories>650</calories>
</food>
POJO - example
• String name;• String price;• String description;• int calories;
• Getters + setters
XML parsing: example
• List<Food> foodList = new ArrayList<>();
• Food food = new Food();• food.setName(name);• food.setDescription(description);• food.setPrice(price);• food.setCalories(Integer.parseInt(calories));
• foodList.add(food);
POJO - practice
• http://joerichard.net/api/ac/countries.xml
• Create list of country POJOs• Print amount of countries using POJO
list
POJO – practice 2
• http://joerichard.net/api/ac/books.xml• Create list of books POJOs• Print amount of books
FINAL PRACTICE
• http://joerichard.net/api/ac/students.xml
• Create DocumentBuilderFactory• Create DocumentBuilder• Create Document• Create node list• Using node list parse XML to POJOs
list• Print names: firstname+lastname
Questions?
• Do you have questions?
Review
• Chapter 10: File management
Next lesson
• Tic Tac Toe
Test: June 29th
• Warmup-1 & Warmup-2 10%+10%• String-1 & String-2 10%+10%• Array-1 & Array-2 10%+10%• Logic-1 & Logic-2 10%+10%• Project Euler 1 problem from 1-20
20%• 8*10%+1*20%=100%• Next month will be FREE!
Thank you
• Thank you for your attention!