導航:首頁 > 編程語言 > 分析java文件

分析java文件

發布時間:2023-07-03 02:34:32

java中怎麼解析這樣的文件

鍵值的話直接獲取節點值, 你補充的那種形式要獲取屬性名和屬性值即可。
用dom4j處理,要下載dom4j.jar包build path到項目里,然後你的xml內容要改一下,裡面用雙引號:
<?xml version="1.0" encoding="UTF-8"?>
<msg>
<head>
<transcode>9999</transcode>
<time>20090305102259</time>
<version> 1.0 </version>
</head>
<body>
<msg errorCode="9001" msg="系統異常" />
</body>
</msg>

代碼

import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class GetXMLMessage {
public static void main(String[] args) throws DocumentException
{
//讀取XML文件,我直接放根目錄下
File file = new File("your.xml");
Document doc = new SAXReader().read(file);
//獲取根節點:即msg節點
Element element =doc.getRootElement();
//遍歷子節點,也即<head>和<body>。
Iterator it = element.elementIterator();
while(it.hasNext())
{
Element subelement = (Element)it.next();
System.out.println("父節點"+subelement.getName());
//遍歷head下面的transcode time cerson 和body下面的msg
Iterator subit = subelement.elementIterator();
while(subit.hasNext())
{
Element subsubelement =(Element) subit.next();
System.out.print("子節點"+subsubelement.getName()+" ");
System.out.println(subsubelement.getStringValue());
//如果有屬性,則輸出屬性
List attributelist = subsubelement.attributes();
for(Object i:attributelist)
{
Attribute attribute =(Attribute)i;
System.out.print("屬性"+attribute.getName()+" ");
System.out.println(attribute.getStringValue());
}

}
}

}

}

最後輸出:

父節點head
子節點transcode 9999
子節點time 20090305102259
子節點version 1.0
父節點body
子節點msg
屬性errorCode 9001
屬性msg 系統異常

閱讀全文

與分析java文件相關的資料

熱點內容
單片機怎麼做人臉識別 瀏覽:148
監獄辦理工商銀行app怎麼辦呢 瀏覽:813
c語言寫編程時需要用什麼輸入法 瀏覽:584
生發程序員 瀏覽:164
高考英語pdf 瀏覽:412
哈利波特忘記伺服器怎麼辦 瀏覽:818
怎麼看其他電腦共享文件夾 瀏覽:507
py文件夾後綴 瀏覽:717
你對我們的app有什麼建議 瀏覽:578
phpgetcookie 瀏覽:141
程序員最煩遇到的單詞 瀏覽:124
開始伺服器升級需要什麼 瀏覽:981
gcc中的編譯選項 瀏覽:189
程序員長沙開滴滴 瀏覽:138
十幾加幾的進位加法演算法 瀏覽:385
c語言實現字母加密成字母 瀏覽:329
linux重啟java服務 瀏覽:54
ubuntu的命令行在哪裡 瀏覽:981
伺服器tk是什麼意思 瀏覽:398
防止軟體加密碼卸載 瀏覽:183