1 import java.io.File; 2 import java.io.IOException; 3 import javax.xml.parsers.DocumentBuilder; 4 import javax.xml.parsers.DocumentBuilderFactory; 5 import javax.xml.parsers.ParserConfigurationException; 6 import org.w3c.dom.Document; 7 import org.w3c.dom.Element; 8 import org.w3c.dom.Node; 9 import org.w3c.dom.NodeList;10 import org.xml.sax.SAXException;11 12 public class XmlTest {13 14 public static void main(String[] args) {15 16 try {17 18 DocumentBuilderFactory builderFactory = DocumentBuilderFactory19 .newInstance();20 DocumentBuilder builder = builderFactory.newDocumentBuilder();21 Document document = builder.parse(new File("NewFile.xml"));22 Element root = document.getDocumentElement();23 System.out.被解析的XML:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <root name="StudentInformation"> 3 <student id="1"> 4 <name>張三</name> 5 <address>北京</address> 6 </student> 7 <student id="2"> 8 <name>李四</name> 9 <address>上海</address>10 </student>11 <student id="3">12 <name>王五</name>13 <address>廣州</address>14 </student>15 </root>
顯示結果:
StudentInformation:-----------------------id= 1name=張三address=北京-----------------------id= 2name=李四address=上海-----------------------id= 3name=王五address=廣州
新聞熱點
疑難解答