Java中如何读取xml文件?
2011-01-22 01:12:321***
Java中如何读取xml文件?:大概有4种方法,这里我用了一个简单例子来说明问题。
xml文件:
Xml代码
<?xml version="1.0" encod?
最佳回答
DocumentBuilder; import rsers。DocumentBuilderFactory; import m。Document; import m。NodeList; public class MyXMLReader2DOM { public static void main(String arge[]) { long lasting = rrentTimeMillis(); try { File f = new File("data_10k。
xml"); DocumentBuilderFactory factory = wInstance(); DocumentBuilder builder = wDocumentBuilder(); Document doc = rse(f); NodeList nl = tElementsByTagName("VALUE"); for (int i = 0; i < tLength(); i++) { int("车牌号码:"+ tElementsByTagName("NO")。
item(i)。getFirstChild()。getNodeValue()); intln("车主地址:"+ tElementsByTagName("ADDR")。item(i)。getFirstChild()。
getNodeValue()); intln("运行时间:" + ( rrentTimeMillis() - lasting) + "毫秒"); } } } catch (Exception e) { intStackTrace(); } } } 详情见: 。
2011-01-22 22:12:18
2011-01-22 22:16:10
*; import 。File; import 。FileInputStream; import 。IOException; import java。util。logging。Level; import java。util。logging。
Logger; import m。*; import rsers。*; import ansform。*; import m。*; import ream。StreamResult; import x。SAXException; public class MyXMLReader { public String libPath = null; public String filePath; public void setFilePath(String filePath) { lePath = filePath; } public String getLibPath() { return libPath; } //得到用户xml中各个节点的值 public void getPathValue(String xmlPath) throws ParserConfigurationException, SAXException, IOException { File f = new File(xmlPath); FileInputStream fs=new FileInputStream(f); DocumentBuilderFactory factory = wInstance(); DocumentBuilder builder = wDocumentBuilder(); Document doc = rse(f); NodeList n2 = tDocumentElement()。
getElementsByTagName("context-param"); for (int i = 0; i < tLength(); i++) { String name = tElementsByTagName("param-name")。
item(i)。getFirstChild()。getNodeValue(); String value = tElementsByTagName("param-value")。item(i)。getFirstChild()。
getNodeValue(); if (name。equals("libPath")) { libPath = value; } } } //将要更新文件中的值替换掉 public void setPathValue() throws ParserConfigurationException, SAXException, IOException { String xml="。
/hzims/Tomcat6/webapps/ROOT/WEB-INF/web。xml"; File f = new File(xml); FileInputStream fs=new FileInputStream(f); DocumentBuilderFactory factory = wInstance(); DocumentBuilder builder = wDocumentBuilder(); Document doc = rse(f); NodeList n2 = tDocumentElement()。
getElementsByTagName("context-param"); for (int i = 0; i < tLength(); i++) { String name = tElementsByTagName("param-name")。
item(i)。getFirstChild()。getNodeValue(); String value = tElementsByTagName("param-value")。item(i)。getFirstChild()。
getNodeValue(); if (name。equals("libPath")) { tElementsByTagName("param-value")。item(i)。getFirstChild()。
setNodeValue(libPath); try { reSetValue(doc,"。/hzims/Tomcat6/webapps/ROOT/WEB-INF/web。
xml" ); } catch (FileNotFoundException ex) { tLogger( tName())。log(Level。SEVERE, null, ex); } catch (TransformerConfigurationException ex) { tLogger( tName())。
log(Level。SEVERE, null, ex); } catch (TransformerException ex) { tLogger( tName())。
log(Level。
SEVERE, null, ex); } } } } public void reSetValue(Document doc, String path) throws FileNotFoundException, IOException, TransformerConfigurationException, TransformerException { TransformerFactory tFactory = wInstance(); Transformer transformer = wTransformer(); //设置输出的encoding为改变gb2312 tOutputProperty("encoding", "gb2312"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(path)); ansform(source, result); } }。
2011-01-22 10:01:26
很赞哦! (104)
相关文章
- XML文件用什么软件打开?扩展名
- JAVA读取xml文件中节点值是
- 用C语言读写xml文件请问怎么用
- 怎样编辑生成xml文件的模板?我
- 如何在IE中打开XML文件我用的
- JAVA中接口具体该怎么使用?
- 在java中如何实现检索一个字符
- xml如何转换xls?xml文件
- 用java语言怎么读取一个文件夹
- 100分跪求如何使用JAVA读取
- java中如何才能在输出数据中间
- 100分跪求如何使用VS系统(c
- Java读取数据库打印到word
- Java高级怎样读取和处理XML
- xml格式文件怎么导入cad中
- 在Java中,如何将.txt文件
- java中super关键字的用法
- java中声明是什么意思?、
- Java访问WebService
- 目录是什么??安装时出现的目录我
- 驾驶证快要到期了,换证都需要准备