JAVA 解析 XMl:DOM SAX ..

  
  
  
  
  1. public class DOMDemo { 
  2.     public static void main(String[] args) { 
  3.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
  4.         try { 
  5.             DocumentBuilder builder = factory.newDocumentBuilder(); 
  6.             File file = new File("src/com/ibatis/jpetstore/persistence/sqlmapdao/sql/sql-map-config.xml"); 
  7.             Document doc = builder.parse(file); 
  8.             Element root = doc.getDocumentElement(); 
  9.             showDom(root, ""); 
  10.         } catch (ParserConfigurationException e) { 
  11.             e.printStackTrace(); 
  12.         } catch (SAXException e) { 
  13.             e.printStackTrace(); 
  14.         } catch (IOException e) { 
  15.             e.printStackTrace(); 
  16.         } 
  17.          
  18.     } 
  19.      
  20.     public static void showDom(Node root, String lpad) { 
  21.          
  22.         //如果是文本节点,输出并返回 
  23.         if(root.getNodeType() == Node.TEXT_NODE){ 
  24.             //如果只有"\n"和" ",不输出。 
  25.             if(false){ 
  26.                 return
  27.             } 
  28.             System.out.println(lpad + root.getNodeValue()); 
  29.             return
  30.         } 
  31.          
  32.         //如果是Element节点,输出 
  33.         if(root.getNodeType() == Node.ELEMENT_NODE){ 
  34.             StringBuilder prex = new StringBuilder(""); 
  35.             prex.append(lpad + "<" + root.getNodeName() + " "); 
  36.             NamedNodeMap attrs =  root.getAttributes(); 
  37.              
  38.             //如果有属性,遍历输出 
  39.             Node attr; 
  40.             for(int i=0; attrs.getLength() > 0 && i < attrs.getLength(); i++){ 
  41.                 attr = attrs.item(i); 
  42.                 prex.append(attr.getNodeName() + "=\"" + attr.getNodeValue() + "\" "); 
  43.             } 
  44.              
  45.             //如果不包含子节点,输出结束标签"/>",并返回 
  46.             if(!root.hasChildNodes()){ 
  47.                 prex.append("/>"); 
  48.                 System.out.println(prex.toString()); 
  49.                 return
  50.             } 
  51.              
  52.             //如果包含子节点,输出结束标签">",并遍历子节点,最后输出关闭标签。 
  53.             prex.append(">"); 
  54.             System.out.println(prex.toString()); 
  55.              
  56.             NodeList children = root.getChildNodes(); 
  57.             for(int i=0; i < children.getLength(); i++){ 
  58.                 showDom(children.item(i), "  " + lpad); 
  59.             } 
  60.              
  61.             System.out.println(lpad + "</" + root.getNodeName() + " >"); 
  62.         } 
  63.     } 

 

<sqlMapConfig >
 

 
  <properties resource="properties/database.properties" />
 

 
  <transactionManager type="JDBC" >
   
   
    <dataSource type="SIMPLE" >
     
     
      <property name="JDBC.Driver" value="${driver}" />
     
     
      <property name="JDBC.ConnectionURL" value="${url}" />
     
     
      <property name="JDBC.Username" value="${username}" />
     
     
      <property name="JDBC.Password" value="${password}" />
     
   
    </dataSource >
   
 
  </transactionManager >
 

 
  <sqlMap resource="com/ibatis/jpetstore/persistence/sqlmapdao/sql/Account.xml" />
 
 
  <sqlMap resource="com/ibatis/jpetstore/persistence/sqlmapdao/sql/Category.xml" />
 
 
  <sqlMap resource="com/ibatis/jpetstore/persistence/sqlmapdao/sql/Product.xml" />
 
 
  <sqlMap resource="com/ibatis/jpetstore/persistence/sqlmapdao/sql/Sequence.xml" />
 
 
  <sqlMap resource="com/ibatis/jpetstore/persistence/sqlmapdao/sql/LineItem.xml" />
 
 
  <sqlMap resource="com/ibatis/jpetstore/persistence/sqlmapdao/sql/Order.xml" />
 
 
  <sqlMap resource="com/ibatis/jpetstore/persistence/sqlmapdao/sql/Item.xml" />
 


</sqlMapConfig >

 

你可能感兴趣的:(java,xml,dom,sax)