JAVA对XML多层次解析
之前需要对XML文件进行读取,在网上查询相关资料的时候查询到的结果都是层次比较低的,无法满足要求,在深入思考之后,实现的对多层次的XML文件进行读取,过程如下:
假设XML文件结构内容如下(采用SP800-53中规定的密钥状态为例)
<states>
<state>
<name>Not Exist</name>
<id>0</id>
<summary>Object that hasn't been created</summary>
<next_state>
<state_id>1</state_id>
</next_state>
</state>
<state>
<name>Pre-Active</name>
<id>1</id>
<summary>state before active</summary>
<next_state>
<state_id>2</state_id>
<state_id>4</state_id>
<state_id>5</state_id>
</next_state>
</state>
<state>
<name>Active</name>
<id>2</id>
<summary>this is state of active</summary>
<next_state>
<state_id>3</state_id>
<state_id>4</state_id>
</next_state>
</state>
<state>
<name>Deactived</name>
<id>3</id>
<summary>state of deactived</summary>
<next_state>
<state_id>4</state_id>
<state_id>5</state_id>
</next_state>
</state>
<state>
<name>Compromised</name>
<id>4</id>
<summary>state of compromised</summary>
<next_state>
<state_id>6</state_id>
</next_state>
</state>
<state>
<name>Destroyed</name>
<id>5</id>
<summary>object in this state cannot be used anymore</summary>
<next_state>
<state_id>6</state_id>
</next_state>
</state>
<state>
<name>Destroy Compromised</name>
<id>6</id>
<summary>Last state</summary>
</state>
</states>
代码如下所示:
package xmldecode;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class statedecoder {
static String filepos = "xmlpos\\state.xml";
public static void main(String[] args){
try{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
Document doc = dbBuilder.parse(filepos);
NodeList nList = doc.getElementsByTagName("state");
System.out.println("Length :: " + nList.getLength());
for(int i = 0; i< nList.getLength() ; i ++){
Element node = (Element)nList.item(i);
String namestr = node.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
String idstr = node.getElementsByTagName("id").item(0).getFirstChild().getNodeValue();
String summarystr = node.getElementsByTagName("summary").item(0).getFirstChild().getNodeValue();
System.out.println(namestr + "\n" + idstr + "\n" + summarystr);
Element stateelem = (Element) node.getElementsByTagName("next_state").item(0);
if(stateelem != null){
NodeList statelist = stateelem.getElementsByTagName("state_id");
for(int j = 0; j < statelist.getLength(); j++){
String nextstatestr = statelist.item(j).getFirstChild().getNodeValue();
System.out.print(nextstatestr);
}
}
System.out.println();
System.out.println();
}
}catch (Exception e) {
System.err.println("ERR :: XML-state decoder error");
e.printStackTrace();
}
}
}
运行结果如下所示:
Length :: 7
Not Exist
0
Object that hasn't been created
1
Pre-Active
1
state before active
245
Active
2
this is state of active
34
Deactived
3
state of deactived
45
Compromised
4
state of compromised
6
Destroyed
5
object in this state cannot be used anymore
6
Destroy Compromised
6
Last state