用java解析XML文件

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationExcep<wbr>tion;</wbr>

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class JieXiXML {

<wbr><wbr><wbr><br><wbr><wbr><wbr>public static void main(String[] args) throws ParserConfigurationExcep<wbr>tion, SAXException, IOException {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>DocumentBuilderFactory bdf = DocumentBuilderFactory.newInstance();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>DocumentBuilder db = bdf.newDocumentBuilder();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Document document = db.parse(new File("user.xml"));<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>NodeList list = document.getElementsByTagName_r("usa");<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>for (int i = 0; i &lt; list.getLength(); i++) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Element element = (Element)list.item(i);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>String name = element.getElementsByTagName_r("name").item(0).getFirstChild().getNodeValue();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(name);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>String pass = element.getElementsByTagName_r("pass").item(0).getFirstChild().getNodeValue();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(pass);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println("------------------");<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr>}</wbr></wbr></wbr>

}

<wbr></wbr>

这是本例子解析的xml文件的内容

<?xml version="1.0" encoding="UTF-8"?>
<user>
<wbr><wbr><wbr>&lt;usa&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;name&gt;张三&lt;/name&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;pass&gt;张三的密码&lt;/pass&gt;<br><wbr><wbr><wbr>&lt;/usa&gt;<br><wbr><wbr><wbr>&lt;usa&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;name&gt;里斯&lt;/name&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;pass&gt;里斯的密码&lt;/pass&gt;<br><wbr><wbr><wbr>&lt;/usa&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

</user>

你可能感兴趣的:(解析xml)