搜索
首页 电脑/网络 互联网 上网帮助

Java中如何读取xml文件?

全部回答

2011-01-22

0 0
    大概有4种方法,这里我用了一个简单例子来说明问题。 xml文件: Xml代码 A1234 河南省郑州市 B1234 河南省郑州市二七区 第一种 DOM 实现方法: Java代码 import 。
  File; import rsers。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

171 0
专业了,顶下,拿分走人。

2011-01-22

199 0
    您可以参考下 * * To change this template, choose Tools | Templates * and open the template in the editor。
   */ package xml; import 。*; 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); } }。

类似问题换一批

热点推荐

热度TOP

相关推荐
加载中...

热点搜索 换一换

电脑/网络
上网帮助
硬件
电脑装机
程序设计
互联网
操作系统/系统故障
笔记本电脑
反病毒
百度
软件
互联网
上网帮助
QQ
网站推荐
上网帮助
上网帮助
举报
举报原因(必选):
取消确定举报