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;
}
}
java解析xml--DOM模型
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.