JAVA对XML多层次解析

JAVA对XML多层次解析

之前需要对XML文件进行读取,在网上查询相关资料的时候查询到的结果都是层次比较低的,无法满足要求,在深入思考之后,实现的对多层次的XML文件进行读取,过程如下:

假设XML文件结构内容如下(采用SP800-53中规定的密钥状态为例)

<states>
    <state>
        <name>Not Existname>
        <id>0id>
        <summary>Object that hasn't been createdsummary>
        <next_state>
            <state_id>1state_id>
        next_state>
    state>
    <state>
        <name>Pre-Activename>
        <id>1id>
        <summary>state before activesummary>
        <next_state>
            <state_id>2state_id>
            <state_id>4state_id>
            <state_id>5state_id>
        next_state>
    state>
    <state>
        <name>Activename>
        <id>2id>
        <summary>this is state of activesummary>
        <next_state>
            <state_id>3state_id>
            <state_id>4state_id>
        next_state>
    state>
    <state>
        <name>Deactivedname>
        <id>3id>
        <summary>state of deactivedsummary>
        <next_state>
            <state_id>4state_id>
            <state_id>5state_id>
        next_state>
    state>
    <state>
        <name>Compromisedname>
        <id>4id>
        <summary>state of compromisedsummary>
        <next_state>
            <state_id>6state_id>
        next_state>
    state>
    <state>
        <name>Destroyedname>
        <id>5id>
        <summary>object in this state cannot be used anymoresummary>
        <next_state>
            <state_id>6state_id>
        next_state>
    state>
    <state>
        <name>Destroy Compromisedname>
        <id>6id>
        <summary>Last statesummary>
    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

你可能感兴趣的:(Java)