易截截图软件、单文件、免安装、纯绿色、仅160KB

通过DOM4J解析XML文件小结

创建XML文件:
public boolean createXML(){
  try{
   Document doc = DocumentHelper.createDocument();
   Element root = doc.addElement("root");
   Element personNode = root.addElement("person");
   Element sonNode = personNode.addElement("fristson");
   sonNode.setText("lk1");
   Element sonNode2 = personNode.addElement("secandtson");
   sonNode2.setText("lk2");
   
   OutputFormat opf = OutputFormat.createPrettyPrint();
   opf.setEncoding("GB2312");
   
   XMLWriter xmlw = new XMLWriter(new FileWriter("d:\\myXML.xml"),opf);
   xmlw.write(doc);
   
   xmlw.close();
   return true;
  }catch(Exception e){
   System.out.println("error: In create XML");
   return false;
  }
 }
以上只是个简单的创建了一个XML文件在D盘下,下边主要是分析XML文件,提取名字和内容:以JAVA项目中,经典的WEB.XML为例
public boolean updateXML(){
  Document doc = null;
  try{
   SAXReader sr = new SAXReader();
   doc = sr.read(new File("d:\\web.xml"));
   Element personRoot = doc.getRootElement();
   Iterator personNode = personRoot.elementIterator();
   while(personNode.hasNext()){
    Element sonNode = (Element)personNode.next();
    List sonNodes = sonNode.elements();
    System.out.println(sonNode.getName());
     for(int i = 0 ;i<sonNodes.size() ; i++){
      System.out.println(i+":"+((Element)sonNodes.get(i)).getName());
      System.out.println(i+":"+((Element)sonNodes.get(i)).getText());
     }


相关文档:

java 操作XML文件(片段)

//create a new Document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.newDocument();

//add root Node
Element noteuser = d.createElement("note-users");
d.appendChil ......

java解析XML的四种方法

XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便。对于XML本身的语法知识与技术细节,需要阅读相关的技术文献,这里面包括的内容有DOM(Document Object Model),DTD(Document Type Definition),SAX(Simple API for XML),XSD(Xml Schema Definition),XSLT(Exten ......

C#对xml的操作实例源码

扩展标记语言XML(eXtensible Markup Language),是由W3C组织制定的。做为用于替代HTML语言的一种新型的标记语言,XML内部有着很多基本标准,XML就是通过与这些相关标准地结合,应用于科学计算、电子出版、多媒体制作和电子商务的。C#作为一种新型的程序语言,是.Net框架的一个重要组成部分,他和XML的关系颇深。本文就从 ......

objective c xml解析

    今天在CC上看到evangel在招人,上面写了一条数据解析,我想应该就是对XML的解析吧,暂且这样理解了,呵呵。下午搜索了一点东西自己弄了一个XML然后读读看看,现在仅仅是读出了一些东西,先保存代码,待后续更新!
    这个是我创建的xml文件,用于测试用的:
<?xml version="1.0" e ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号