Android Studio XML Parsing DOM
Syntax
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xmlString)));
NodeList nodeList = document.getElementsByTagName("tagName");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element element = (Element) node;
String value = element.getAttribute("attributeName");
}
Example
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
</catalog>
Output
Author : Gambardella, Matthew
Title : XML Developer's Guide
Genre : Computer
Price : 44.95
Publish Date : 2000-10-01
Description : An in-depth look at creating applications with XML.
Author : Ralls, Kim
Title : Midnight Rain
Genre : Fantasy
Price : 5.95
Publish Date : 2000-12-16
Description : A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.
Author : Corets, Eva
Title : Maeve Ascendant
Genre : Fantasy
Price : 5.95
Publish Date : 2000-11-17
Description : After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.
Explanation
XML Parsing DOM is used to parse XML documents into tree-like structures so that they can be easily navigated and manipulated programmatically. The process of parsing an XML document involves creating a DocumentBuilderFactory, DocumentBuilder, and Document object. Once you have a Document object, you can use its methods to traverse and manipulate the XML document.
In the example, we parse a sample XML document that represents a catalog of books. First, we create a DocumentBuilderFactory and DocumentBuilder object. Then, we parse an XML String using the builder's parse method to get a Document object. Next, we retrieve a list of all book elements using the getElementsByTagName method. Finally, we loop through each book element and retrieve its child elements' text values and attributes using Element methods like getAttribute and getChildNodes.
Use
XML Parsing DOM is used in Android app development to parse XML documents retrieved from web services or stored locally in the app's resources. It allows developers to extract data from these documents and use it to populate app views or perform business logic.
Important Points
- XML Parsing DOM is a standard Java API for parsing XML documents.
- The process of parsing an XML document involves creating a DocumentBuilderFactory, DocumentBuilder, and Document object.
- With a Document object, you can traverse and manipulate the XML document using DOM methods.
- In Android, XML Parsing DOM is commonly used to parse XML documents retrieved from web services or stored locally in the app's resources.
Summary
XML Parsing DOM is an essential tool for Android app development. It enables developers to parse XML documents and extract data from them efficiently. By creating tree-like structures, developers can navigate, manipulate, and use the data to populate app views or perform business logic. Given its ubiquity in web services, proficiency in this API is highly valued in the app development community.