package com.test; import java.io.*;import java.util.*;import org.w3c.dom.*;import javax.xml.parsers.*; public class MyXMLReader{ public static void main(String arge[]){ long lasting =System.currentTimeMillis(); try{ File f=new File("data_10k.xml"); DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document doc = builder.parse(f); NodeList nl = doc.getElementsByTagName("VALUE"); for (int i=0;i<nl.getLength();i++){ System.out.print("车牌号码:" + doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue()); System.out.println("车主地址:" + doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue()); } }catch(Exception e){ e.printStackTrace(); }
解决方案 »
- DWR3中getScriptSessionsByPage已过时,请问现在用什么方法?
- 谁用过struts2的验证validation.xml
- Tomcat 总是重启 请大虾帮助 看日志说是 ntdll.dll 有问题
- hibernate.jdbc.fetch_size AND hibernate.jdbc.batch_size最佳是多少?
- 有关ejb的问题
- itext生成PDF报错java.io.IOException: The document has no pages.
- 高手来看看~~
- tomcat上开发的Web应用部署到Websphere上的报错!
- 急!!!在线等 达人请进 急!!!
- 关于代码中写集成测试的问题
- 利用jdom如何向已有的xml中添加下面内容?(在线等)
- 一个关于jsp中使用xslt时的中文显示问题
我得程序:
import java.io.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
public class testxmldom
{
static void printxml (Element root,String stmp)
{
int n1,n2;
System.out.print(stmp+"<"+root.getTagName());//分析属性 beging,用 root.getAttribute("name") 可以明文取值
NamedNodeMap mlist=root.getAttributes();
if (mlist!=null)
{
for (n1=0;n1<mlist.getLength();n1++)
{
System.out.print(" "+mlist.item(n1).getNodeName()+"=\""+mlist.item(n1).getNodeValue()+"\"");
}
}
//分析属性 end
System.out.println(">");
NodeList nlist=root.getChildNodes();
//NodeList ages = student.getElementsByTagName("年龄");
if (nlist!=null)
{
for(n1=0;n1<nlist.getLength();n1++)
{
if (nlist.item(n1).getNodeType()==3) //3是text类型
System.out.println(stmp+nlist.item(n1).getNodeValue().trim());
//1说明是一个子节点
if (nlist.item(n1).getNodeType()==1) printxml((Element)nlist.item(n1),stmp+" "); }
} System.out.println(stmp+"</"+root.getTagName()+">");
}
public static void main (String argv [])
throws IOException, DOMException, ParserConfigurationException,org.xml.sax.SAXException
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
DocumentBuilder db = dbf.newDocumentBuilder ();
Node nodetmp;
Document doc =db.parse(new File("c:/2.xml"));
Element root= doc.getDocumentElement();
/*
Element cc=doc.createElement("add");
cc.setAttribute("name","text");
cc.appendChild(doc.createTextNode("this is test"));
root.appendChild(cc);
*/
printxml(root,"");
System.exit (0);
}}