个人信息
![]() |
|
| zhaoshouzhong(zhaoshouzhong) | |
| 状态: | |
| 等级: | |
| 性别:男 | 年龄: 22 |
| 城市: 保密 | |
| 签名: | |
我的相册
最新日志
| ·软件核心竞争力(2006-01-12) |
| ·微内核研究(2006-01-12) |
| ·XPDL2.0正式版发布(2005-11-22) |
| ·基于基线的版本管理和质量控制(2005-11-13) |
| ·计划管理(2005-11-13) |
我的评论
| ·访客167667(访客)/2006-11-03 |
| 小日本真过分,视中.... |
| ·访客921058/2006-07-20 |
| 你好,您研究Pro.... |
| ·访客248125/2006-03-27 |
| 同意 |
| ·访客427256/2006-02-14 |
| 加油哦! 顺便告诉你.... |
我的日历
我的链接
| 2005-08-26 11:45:48 |
| jaxp DOM解析 |
| <?xml version="1.0" encoding="UTF-8"?> <Database> <server name="andrew">work64</server> <database>wiring</database> <user>sa</user> <password></password> </Database> 这个xml文件,JAXP是如何解析的. DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(files); //获取根元素 Element root = doc.getDocumentElement(); 那么root的第一个节点是什么呢?<server>节点?错了,是位于<Database>和<server name="andrew">work64</server>的空白,文本节点.不信,你打印一下就可以知道:System.err.println("begin:"+root.getFirstChild().getNodeValue()+":end"); 则打印出: begin: :end.其实就是一个回车,换行符.这一点最容易让人弄错.一个DOM包括document(根节点),element(元素节点),text(文本节点),comment(注释节点),entity(实体节点)等. 对xml的解析脚本如下: import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /** Description: @author:Andrew 2005-8-26 */ public class Test { public void parseXml(String file){ DocumentBuilderFactory xmlfactory=null; DocumentBuilder xmlbuild=null; Document doc=null; xmlfactory= DocumentBuilderFactory.newInstance(); xmlfactory.setIgnoringElementContentWhitespace(true); //解析xml文件 try { xmlbuild = xmlfactory.newDocumentBuilder(); doc=xmlbuild.parse(file); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //读取配置信息,给字段赋值 Node root = doc.getDocumentElement(); System.err.println("begin:"+root.getFirstChild().getNodeValue()+":end"); NodeList child = root.getChildNodes(); for(int i=0;i<child.getLength();i++){ Node node=child.item(i); if (node.getNodeType()==Node.ELEMENT_NODE){ if (node.getNodeName().equals("server")){ if(node.getChildNodes().getLength()>0){ String sserver=node.getFirstChild().getNodeValue(); System.err.println(sserver+":::"+i); } } if(node.getNodeName().equals("database")){ if(node.getChildNodes().getLength()>0){ String sdb=node.getFirstChild().getNodeValue(); System.err.println(sdb); } } if(node.getNodeName().equals("user")){ if(node.getChildNodes().getLength()>0){ String suser=node.getFirstChild().getNodeValue(); System.err.println(suser); } } if(node.getNodeName().equals("password")){ if(node.getChildNodes().getLength()>0){ String spass=node.getFirstChild().getNodeValue(); System.err.println(spass); } else System.err.println("null value"); } } else{ System.err.println(node.getNodeName()+"--"+node.getNodeValue()); } } } public static void main(String[] args) { // TODO Auto-generated method stub Test a = new Test(); a.parseXml("configure.xml"); } } |
| 标签: jaxp DOM解析 |
