我写了一个数据库的xml ,然后用java解析,附上代码如下:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE datasource [<!ELEMENT datasource (drive,url,user,password,sql)>
<!ELEMENT drive (#PCDATA)>
<!ELEMENT url (#PCDATA)>
<!ELEMENT user (#PCDATA)>
<!ELEMENT password (#PCDATA)>
<!ELEMENT sql (#PCDATA)>]><datasource>
   <drive>com.mysql.jdbc.Driver</drive>
   <url>jdbc:mysql://127.0.0.1:3306/ums</url>
   <user>root</user>
   <password></password>
   <sql>select * from ums</sql>
</datasource>java程序如下:
public class ReadXml {
  public ReadXml(){
  System.out.println("start");
  DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
 
  try {
DocumentBuilder dombuilder=domfac.newDocumentBuilder();

  InputStream is=new FileInputStream("DB.xml");
 
  Document doc=dombuilder.parse(is);
  
          Element root=doc.getDocumentElement();
          
          NodeList datasource=root.getChildNodes();
          
          if(datasource!=null)
          {
              for(int i=0;i<datasource.getLength();i++)
              { 
                  Node ds=datasource.item(i);                  if(ds.getNodeType()==Node.ELEMENT_NODE)
                  {    
                      for(Node node=ds.getFirstChild();node!=null;node=node.getNextSibling())
                      {   System.out.print(node.getNodeName());
                          if(node.getNodeType()!=Node.ELEMENT_NODE)
                          {   
                           
                              if(node.getNodeName().equals("drive"))
                              {                                   
                                  String drive=node.getFirstChild().getNodeValue();
                                  System.out.println("drive: "+drive);                               
                              }
                              if(node.getNodeName().equals("url"))
                              {
                                  String url=node.getFirstChild().getNodeValue();
                                  System.out.println("url"+url);
                              }
                              if(node.getNodeName().equals("user"))
                              {
                                  String user=node.getFirstChild().getNodeValue();
                                  System.out.println("user"+user);
                              }
                              if(node.getNodeName().equals("password"))
                              {
                                  String password=node.getFirstChild().getNodeValue();
                                  System.out.println("password"+password);
                              }
                              if(node.getNodeName().equals("sql"))
                              {
                                  String sql=node.getFirstChild().getNodeValue();
                                  System.out.println("sql"+sql);
                              }
                          }
                      }
                  }
              }
          }else{
           System.out.println("null");
          }
      
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 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();
}
  }
  public static void main(String[] args)
  {
      new ReadXml();
  }
}执行的总是没有结果,好像NODENAME=#TEXT   大家看看是怎么回事??

解决方案 »

  1.   

    这下应该好使了,在我机器上已经通过了。
    代码如下:import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;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;public class ReadXml { 
      public ReadXml(){ 
      System.out.println("start"); 
      DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();   try { 
    DocumentBuilder dombuilder=domfac.newDocumentBuilder();   InputStream is=new FileInputStream("DB.xml");   Document doc=dombuilder.parse(is); 
      
      
              Element root=doc.getDocumentElement(); 
              NodeList datasource = root.getChildNodes();
              if(datasource!=null) 
              { 
                  for(int i=0;i <datasource.getLength();i++) 
                  { 
                      Node ds=datasource.item(i); 
                      
                      if(ds.getNodeType()==Node.ELEMENT_NODE) 
                      {    
                          for(Node node=ds.getFirstChild();node!=null;node=node.getNextSibling()) 
                          {  System.out.println(ds.getNodeName()); 
                              if(node.getNodeType()!=Node.ELEMENT_NODE) 
                              {  
                              
                                  if(ds.getNodeName().equals("drive")) 
                                  {                                  
                                      String drive=ds.getFirstChild().getNodeValue(); 
                                      System.out.println("drive: "+drive);                              
                                  } 
                                  if(ds.getNodeName().equals("url")) 
                                  { 
                                      String url=ds.getFirstChild().getNodeValue(); 
                                      System.out.println("url: "+url); 
                                  } 
                                  if(ds.getNodeName().equals("user")) 
                                  { 
                                      String user=ds.getFirstChild().getNodeValue(); 
                                      System.out.println("user: "+user); 
                                  } 
                                  if(ds.getNodeName().equals("password")) 
                                  { 
                                      String password=ds.getFirstChild().getNodeValue(); 
                                      System.out.println("password: "+password); 
                                  } 
                                  if(ds.getNodeName().equals("sql")) 
                                  { 
                                      String sql=ds.getFirstChild().getNodeValue(); 
                                      System.out.println("sql: "+sql); 
                                  } 
                              } 
                          } 
                      } 
                  } 
              }else{ 
              System.out.println("null"); 
              } 
          
    } catch (FileNotFoundException e) { 
    e.printStackTrace(); 
    } 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(); 

      } 
      public static void main(String[] args) 
      { 
          new ReadXml(); 
      } 

      

  2.   

    you will find it's easy with org.jdom.*;
      

  3.   

    用dom4j 解析xml 很方便
    ibm 的文档 可以看看
    http://www.ibm.com/developerworks/cn/xml/x-dom4j.html