- public class DOMDemo {
- public static void main(String[] args) {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- try {
- DocumentBuilder builder = factory.newDocumentBuilder();
- File file = new File("src/com/ibatis/jpetstore/persistence/sqlmapdao/sql/sql-map-config.xml");
- Document doc = builder.parse(file);
- Element root = doc.getDocumentElement();
- showDom(root, "");
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void showDom(Node root, String lpad) {
- //如果是文本节点,输出并返回
- if(root.getNodeType() == Node.TEXT_NODE){
- //如果只有"\n"和" ",不输出。
- if(false){
- return;
- }
- System.out.println(lpad + root.getNodeValue());
- return;
- }
- //如果是Element节点,输出
- if(root.getNodeType() == Node.ELEMENT_NODE){
- StringBuilder prex = new StringBuilder("");
- prex.append(lpad + "<" + root.getNodeName() + " ");
- NamedNodeMap attrs = root.getAttributes();
- //如果有属性,遍历输出
- Node attr;
- for(int i=0; attrs.getLength() > 0 && i < attrs.getLength(); i++){
- attr = attrs.item(i);
- prex.append(attr.getNodeName() + "=\"" + attr.getNodeValue() + "\" ");
- }
- //如果不包含子节点,输出结束标签"/>",并返回
- if(!root.hasChildNodes()){
- prex.append("/>");
- System.out.println(prex.toString());
- return;
- }
- //如果包含子节点,输出结束标签">",并遍历子节点,最后输出关闭标签。
- prex.append(">");
- System.out.println(prex.toString());
- NodeList children = root.getChildNodes();
- for(int i=0; i < children.getLength(); i++){
- showDom(children.item(i), " " + lpad);
- }
- System.out.println(lpad + "</" + root.getNodeName() + " >");
- }
- }
- }
<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 > |