今晚聊了一晚天,边看java 边聊天,没有集中精神去看。没搞懂java读取xml 的机制。看了一些书先把他记录一下。采用DOM文档对象模型,第一种方法来解析。听说这种解析会有一些缺点,不过总算调试通了,算是一种进步。
<?xml version="1.0" encoding="utf-8"?>
<item>
<node>aa</node>
<node>bb</node>
</item>
代码列表。
public class HelloWorld
{
public static void main(String arge[]) throws Exception
{
File f=new File("test.xml"); //文件加载xml
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList node = doc.getElementsByTagName("node");//
System.out.print( doc.getElementsByTagName("node").item(1).getFirstChild().getNodeValue());
}
}