Android dom parses xml method
First create an xml file yourself: DomTest.xml
<?xml version="1.0" encoding="utf-8"?> <classes> <group name="一年级" num="10"> <person name="小明" age="7"> <chinese> 语文80 </chinese> <english> 英语89 </english> </person> <person name="小强" age="8"> <chinese> 语文90 </chinese> <english> 英语99 </english> </person> </group> <group name="二年级" num="20"> <person name="小文" age="8"> <chinese> 语文85 </chinese> <english> 英语95 </english> </person> <person name="小中" age="9"> <chinese> 语文80 </chinese> <english> 英语90 </english> </person> </group> </classes>
The parsed result is shown as follows:

Let’s analyze the source code:
/**
* 用dom方式 解析xml 文件
* @param fileName
*/
private String domXmlParse(String fileName) {
String str="";
// xml文档创建工厂
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
// xml文档创建实例
DocumentBuilder docBuilder;
// xml文档
Document doc = null;
InputStream inStream = null;
try {
docBuilder = docFactory.newDocumentBuilder();
// 从assets文件夹下获取文件 转换成输入流
inStream = this.getResources().getAssets().open(fileName);
doc = docBuilder.parse(inStream);
// 获取xml跟元素
Element rootEle = doc.getDocumentElement();
// 二级父元素的list列表
NodeList groupNode = rootEle.getElementsByTagName("group");
// NodeList childNode = rootEle.getElementsByTagName("person");
// 遍历Classe下所有的group
for (int i = 0; i < groupNode.getLength(); i++) {
Element groupEle = (Element) groupNode.item(i);
String groupName = groupEle.getAttribute("name");
String num = groupEle.getAttribute("num");
str =str+"name ="+groupName+" num = "+num+"\n";
Log.e("xml", "name = " + groupName + " num = " + num);
// NodeList personNode = groupNode.item(i).getChildNodes();
NodeList personNode = groupEle.getElementsByTagName("person");
// 遍历group下的所有person
for (int j = 0; j < personNode.getLength(); j++) {
Element personEle = (Element) personNode.item(j);
String name = personEle.getAttribute("name");
String age = personEle.getAttribute("age");
str =str+"personName ="+name+" personAge = "+age+"\n";
Log.e("xml", "name = " + name + " age = " + age);
Element chineseEle = (Element) personEle
.getElementsByTagName("chinese").item(0);
Element englistEle = (Element) personEle
.getElementsByTagName("english").item(0);
String chinese = chineseEle.getFirstChild().getNodeValue();
String english = englistEle.getFirstChild().getNodeValue();
str =str+"chinese = "+chinese+" english = "+english+"\n";
Log.e("xml", "chinese = " + chinese + " english = "
+ english);
}
}
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return str;
}Defines a set of interfaces for the parsed version of an XML document. The parser reads in the entire document and builds a memory-resident tree structure that code can then manipulate using the DOM interface. Advantages: The entire document tree is in memory, easy to operate; supports multiple functions such as deletion, modification, and rearrangement; Disadvantages: Transferring the entire document into memory (including useless nodes) wastes time and space; Usage occasions: Once parsed Documents also need to access this data multiple times; hardware resources are sufficient (memory, CPU).
The above is the content of the android dom parsing xml method. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20446
7
13592
4
How to change the song cache directory in NetEase Cloud Music
Jan 13, 2026 am 10:30 AM
In the process of using NetEase Cloud Music, you occasionally need to adjust the song cache path, for example, if the original cache location is running out of disk space, or you want to store all cached files in a designated folder. The following will give you a clear explanation on how to modify the song cache path of NetEase Cloud Music. Launch the NetEase Cloud Music App on the Android system on the mobile phone. Click the "≡" three horizontal lines icon in the upper left corner to bring up the side menu bar. Swipe to find and click "Settings" in the sidebar. After entering the settings page, locate the "Storage Path" option. The cache folder currently displayed is the cache folder in use. Click "Storage Path", and the system will bring up the local file manager, allowing you to select any folder in the built-in storage of the fuselage, or a directory in the mounted SD card. select
Where to download the latest version of iMoutai? iMoutai official app safety installation guide
Jan 13, 2026 pm 04:42 PM
The official download entrance for the latest version of iMoutai v1.9.0 is the official website https://www.moutaichina.com/, which supports dual-end downloads for Android and iOS. The installation package is transmitted through SSL encryption and SHA256 verification to ensure security and trustworthiness.
Devin AI: Disrupting software engineering? An in-depth analysis of the future of AI software engineers
Jan 12, 2026 pm 10:54 PM
In 2025, artificial intelligence (AI) is penetrating into various industries at an alarming rate, and the field of software engineering is no exception. Recently, an AI software engineer named DevinAI emerged, which has attracted widespread attention in the industry. DevinAI claims to be able to complete software development tasks autonomously, which has triggered concerns and discussions about whether AI will replace traditional software engineers. This article
What to do if the text layout of the Quark browser webpage is confusing? Quark browser page adaptation tutorial
Jan 15, 2026 pm 02:24 PM
When Quark Browser's web page layout is disordered, it can be repaired by enabling "Web Page Adaptation Optimization", disabling hardware acceleration, clearing font and CSS cache, switching the UA logo, and turning off script interception.
LiveKit and Python: Create Jarvis AI voice assistant for free
Jan 13, 2026 am 06:21 AM
In science fiction movies, we often see intelligent voice assistants. They can not only understand our instructions, but also complete various complex tasks. Now, with the help of open source tools LiveKit and Python, you can also create your own JarvisAI voice assistant for free! This article will take you step by step to understand the advantages of LiveKit and learn how to set up L
The Ultimate Guide to AI Photo Editing: Create Social Media Hits Easily
Jan 08, 2026 pm 06:54 PM
In today's age of social media, a striking photo can often determine your online presence. Do you also want to have a photo that can quickly attract attention and attract attention? Now, AI technology makes this easier than ever. This blog will give you an in-depth understanding of how to use AI tools, especially Google Gemini AI, to easily edit photos and create stunning
What to do if the cache of Zeus Browser takes up too much? Clean the cache of Zeus Browser
Jan 10, 2026 pm 12:36 PM
Zeus Browser runs slowly, the page loads abnormally, or a large amount of storage space is occupied, which is probably caused by the backlog of cache files. It can be cleaned through five methods: the main interface shortcut entry, privacy settings, traffic consumption module, system-level shortcut keys, or manually deleting the local cache directory.
How to set the default QR code generation method in Win11_Win11 shared link automatic transcoding associated application [Tool]
Jan 10, 2026 pm 01:33 PM
It is necessary to enable the system QR code function through ViVeTool, set the default sharing to Windows sharing, the WiFi password can directly generate the QR code in the WLAN settings, and fix icon abnormalities caused by high contrast or third-party startup items.




