java解析xml--DOM模型

package com.edu.hdu;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class XMLRe {

	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		//当node.getNodeName()==#text时,node不能强制转化为Element
       Document document=openFile("D:\\aa.xml");//得到文件根结点
        Element root= (Element)document.getElementsByTagName("FrameworkConfig").item(0);
        String str="";
        NodeList nodeList=root.getChildNodes();//得到所有子节点
        //白空节点name是#text,注释name是#comment,标签name就是标答名
        for(int i=0;i<nodeList.getLength();i++){
//        	Element node=(Element)nodeList.item(i);
//        	System.out.println("caption="+node.getAttribute("Caption"));
        	Node node=nodeList.item(i);
        	if(node.getNodeName()=="#comment")
        		str+="<!--"+node.getNodeValue()+"-->\n";
        	else if(node.getNodeName()=="GUI"){
        		NamedNodeMap nodemap=node.getAttributes();//返回节点属性集合
        		String keyword=nodemap.getNamedItem("Keyword").getNodeValue();
        		str+="<UUI Keyword="+'"'+keyword+'"'+" ID="+'"'+"TV"+'"'+">";
        		str+=resolve(node);
        		str+="</UUI>\n";
        	}	
        }
        System.out.println(str);
        save("D:\\11.xml",str);
	}
	public static String resolve(Node node){
		String str="\n";
		Element root=(Element)node;
		NodeList nodeList=root.getElementsByTagName("GUI");
		//System.out.println(""+nodeList.getLength());
		for(int i=0;i<nodeList.getLength();i++){
			Node no=nodeList.item(i);
			//System.out.println(no.getNodeName());
			NamedNodeMap nodemap=no.getAttributes();
			String type=nodemap.getNamedItem("Type").getNodeValue();
		//	System.out.println(type);
			if(type.equals("Button")||type.equals("PushButton")||type.equals("Panel")||
					type.equals("ScrollBar")){
				String caption=nodemap.getNamedItem("Caption").getNodeValue();
				String keyword=nodemap.getNamedItem("Keyword").getNodeValue();
				str+="  <UUI Keyword="+'"'+keyword+'"'+" TV=555 /> <!--"+caption+"-->\n" ;
				
			}
		}
		return str;	
	}
	public static boolean save(String filePath,String fileString){
		
		try {
			FileOutputStream os=new FileOutputStream(new File(filePath));
			os.write(fileString.getBytes());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	public static Document openFile(String filePath){
		// TODO Auto-generated method stub
				 // step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器)  
		        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();            
		        // step 2:获得具体的dom解析器  
		        DocumentBuilder db;
				try {
					db = dbf.newDocumentBuilder();
					// step3: 解析一个xml文档,获得Document对象(根结点)  
					 Document document = db.parse(new File(filePath)); 
					 return document;
				} 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();
				}
				return null;
				 
		       
	}

}

你可能感兴趣的:(xml,dom,文档)