org.apache.commons.digester.Digester类提供了解析XML数据的功能,可以参阅它的API文档。

解决方案 »

  1.   

    package configtools;/**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2005</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */
    import javax.xml.parsers.*;
    import java.io.*;
    import org.w3c.dom.*;
    import org.xml.sax.*;/*
    * Created on 2004-6-2
    *java读取XML文档
    *利用DoM来读取一个XML文档的内容,并将其打印出来
    */public class DomParse  {
      public DomParse(){
        DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
         try {
           DocumentBuilder dombuilder=domfac.newDocumentBuilder();
           InputStream is=new FileInputStream("d:\\DAOConfigzsl.xml");
           Document doc=dombuilder.parse(is);
           Element root=doc.getDocumentElement();//得到xml文档的根节点
           NodeList tasks=root.getChildNodes();     if(tasks!=null){//得到子节点
           for(int i=0;i<tasks.getLength();i++){
             Node task=tasks.item(i);
             if(task.getNodeType()==Node.ELEMENT_NODE){//
               //String email=task.getAttributes().getNamedItem("email").getNodeValue();
              // System.out.println(email);
               for(Node node=task.getFirstChild();node!=null;node=node.getNextSibling()){
                 if(node.getNodeType()==Node.ELEMENT_NODE){
                   if(node.getNodeName().equalsIgnoreCase("name")){
                     String name=node.getFirstChild().getNodeValue();
                     System.out.println(name);
                   }
                 if(node.getNodeName().equalsIgnoreCase("database_name")){
                   String database_name=node.getFirstChild().getNodeValue();
                   System.out.println(database_name);
                  }
                  if(node.getNodeName().equalsIgnoreCase("type")){
                    String type=node.getFirstChild().getNodeValue();
                    System.out.println(type);
                   }
                   if(node.getNodeName().equalsIgnoreCase("batch")){
                     String batch=node.getFirstChild().getNodeValue();
                     System.out.println(batch);
                   }
                   if(node.getNodeName().equalsIgnoreCase("transaction")){
                     String transaction=node.getFirstChild().getNodeValue();
                     System.out.println(transaction);
                   }
                   if(node.getNodeName().equalsIgnoreCase("executor")){
                     //System.out.println("+++++++++++"+ node.getNodeName());
                     if (node.getChildNodes().getLength() != 0) {
                       String executors = node.getFirstChild().getNodeValue();
                       //System.out.println("123");
                       System.out.println("EXECUTORS:" + executors);
                     }
                     else{
                       System.out.println("EXECUTORS: ");
                     }
                   }
                   if(node.getNodeName().equalsIgnoreCase("description")){
                     String description=node.getFirstChild().getNodeValue();
                     System.out.println(description);
                   }
                   if(node.getNodeName().equalsIgnoreCase("SQL")){//获取sql的子节点
                     NodeList sqlNodes=node.getChildNodes();                 System.out.println("********"+sqlNodes.item(0).getNodeValue());
                     System.out.println("");
                     System.out.println("=========="+sqlNodes);
                     for(int j=0;j<sqlNodes.getLength();j++){
                       Node sqlNode=sqlNodes.item(j);
                       for(Node node1=sqlNode.getFirstChild();node1!=null;node1=node1.getNextSibling()){
                       if (node1.getNodeType() == Node.ELEMENT_NODE) {
                         if (node1.getNodeName().equalsIgnoreCase("name")) {
                           String name = node1.getFirstChild().getNodeValue();
                           System.out.println(name);
                         }
                         if (node1.getNodeName().equalsIgnoreCase("type")) {
                           String type = node1.getFirstChild().getNodeValue();
                            System.out.println(type);
                         }
                         if (node1.getNodeName().equalsIgnoreCase("clause")) {
                           String clause = node1.getFirstChild().getNodeValue();
                           if (!clause.equals("")) {
                             System.out.println(clause);
                           }
                            else {
                             String update_clause = node1.getFirstChild().getFirstChild().getNodeValue();
                             System.out.println(update_clause);
                             String select_clause = node1.getFirstChild().getLastChild().getNodeValue();
                             System.out.println(select_clause);
                           }                     } //end of clause
                         if(node.getNodeName().equalsIgnoreCase("parameter_type_list")){
                                         System.out.println("++++++++++++++"+node.getNodeName());
                                         String parameter_type_list=node.getFirstChild().getNodeValue();
                                         System.out.println(parameter_type_list);
                          }
                          if(node.getNodeName().equalsIgnoreCase("description")){
                           String description=node.getFirstChild().getNodeValue();
                           System.out.println(description+"\n");
                }                   }//end of if (node1.getNodeType() == Node.ELEMENT_NODE)
                     }//end of for
                   }//end of sql
                }//end of if (node.getNodeType() == Node.ELEMENT_NODE)          }//end of for
            }//end of if(task.getNodeType()==Node.ELEMENT_NODE)
          }//end of for(int i=0;i<tasks.getLength();i++){
          }// end of if(tasks!=null)
         }
        }//end of try
         catch (ParserConfigurationException e) {
             e.printStackTrace();
          } catch (FileNotFoundException e) {
            e.printStackTrace();
           } catch (SAXException e) {
              e.printStackTrace();
           } catch (IOException e) {
            e.printStackTrace();
           }
           }//end
    public static void main(String[] args) {
      new DomParse();
     }
     }
    我的程序是这样的,但是最后的sql里面的节点都解析不出来。不知道是怎么回事。
      

  2.   

    很普通得到一个XML文件,使用JDOM很简单,我给你个示例代码:
    test.xml文件内容如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <books>
       <book email="[email protected]">
         <name>zuochuanmin</name>
         <price>100dollars</price>
      <msn>[email protected]</msn><msn>[email protected]</msn></book>
         <book email="[email protected]">
         <name>zuochuanmin</name>
         <price>100dollars</price>
      <msn>[email protected]</msn><msn>[email protected]</msn></book>
         <book email="[email protected]">
         <name>zuochuanmin</name>
         <price>100dollars</price>
      <msn>[email protected]</msn><msn>[email protected]</msn></book>
    </books>解析的代码:
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Iterator;
    import java.util.List;
    //下面是引用到JDOM中的CLASS
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.input.SAXBuilder;
    import org.jdom.output.XMLOutputter;/**
     * @author Administrator
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class JDomParse {
        public JDomParse(){
                
             String xmlpath="test.xml";
                //使用JDOM首先要指定使用什?解析器。
             SAXBuilder builder = new SAXBuilder(false);//表示使用的是DEFAULT解析器
             try {
                 //得到Document,我?以后要?行的所有操作都是??个Document操作的
             Document doc=builder.build(xmlpath);
             Element books=doc.getRootElement();
             List booklist=books.getChildren("book");
             for (Iterator iter = booklist.iterator(); iter.hasNext();) {
             Element book = (Element) iter.next();
                String email=book.getAttributeValue("email");
             System.out.println(email);
             String name=book.getChildTextTrim("name");
             String price=book.getChildTextTrim("price");
             System.out.println(name);
             System.out.println(price);
             //?只是?Document的修改,并没有在??的XML文档中?行修改
             book.getChild("name").setText("zuochuanmin");
             book.getChild("price").setText("100dollars");
             book.addContent(new Element("msn").setText("[email protected]"));
            }
                //保存Document的修改到XML文件中
             XMLOutputter outputter=new XMLOutputter();
             outputter.output(doc,new FileOutputStream(xmlpath));
            
            } catch (JDOMException e) {
             e.printStackTrace();
            } catch (IOException e) {
             e.printStackTrace();
             }
             }
             public static void main(String[] args) {
             new JDomParse();
             }}
      

  3.   

    解析一个xml文档,基本可以看成以下几个步骤:
        1.实例化一个合适的解析器对象
            本例中我们使用SAXBuilder:
            SAXBuilder sb = new SAXBuilder();
        2.以包含XML数据的文件为参数,构建一个文档对象myDocument
            Document myDocument = sb.build(/some/directory/myFile.xml);
        3.获到根元素
            Element rootElement = myDocument.getRootElement();
            
            一旦你获取了根元素,你就可以很方便地对它下面的子元素进行操作了,下面对Element对象的一些常用方法作一下简单说明:
            getChild("childname") 返回指定名字的子节点,如果同一级有多个同名子节点,则只返回第一个;如果没有返回null值。
            getChildren("childname") 返回指定名字的子节点List集合。这样你就可以遍历所有的同一级同名子节点。 
            getAttributeValue("name") 返回指定属性名字的值。如果没有该属性则返回null,有该属性但是值为空,则返回空字符串。
            getChildText("childname") 返回指定子节点的内容文本值。
            getText() 返回该元素的内容文本值。
            
            还有其他没有罗列出来的方法,如果需要的话,可以随时查阅JDOM的在线文档:http://www.jdom.org/docs/apidocs/index.html。
      

  4.   

    推荐Dom4j 这个感觉比较好用
      

  5.   

    用axis开发服务时,axis不支持jdom序列化的。需要自己开发序列化工具