3

SAX パーサーを使用して xml ファイルを解析しようとしています。属性を取得する必要があり、それは開始要素の値です

<?xml version="1.0" encoding="ISO-8859-1" ?>
<API type="Connection">
<INFO server="com.com" function="getAccount2" />
<RESULT code="0">Operation Succeeded</RESULT>
<RESPONSE numaccounts="1">
<ACCOUNT login="fa051981" skynum="111111" maxaliases="1" creationdate="Fri Nov 16 00:59:59 2001"    password="pass" type="2222" status="open" mnemonic="32051981" ratelimit="0">
    <CHECKATTR />
    <REPLYATTR>Service-Type = Frames-User, Framed-Protocol = PPP, Framed-Routing = None</REPLYATTR>
    <SETTINGS bitval="4" status="open" />
    <SETTINGS bitval="8192" status="open" session_timeout="10800" />
    <SETTINGS bitval="32768" status="open" cisco_address_pool="thepool" />
    <ALIASES numaliases="0" />
</ACCOUNT>
</RESPONSE>
</API>

この xml では、設定タグ/開始要素の属性とその値を取得する必要があります。

これらの属性は動的であるため、それらのマップを作成しようとしています。私は SAX パーサーを初めて使用します。

これまでのところ、私のJavaコード:

public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException {

    if (elementName.equalsIgnoreCase(GenericConstants.INFO)) {
        this.searchRaidusBean.setServer(attributes.getValue(GenericConstants.SERVER));
        this.searchRaidusBean.setFunction(attributes.getValue(GenericConstants.FUNCTION));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.RESULT)) {
        this.searchRaidusBean.setResultCode(attributes.getValue(GenericConstants.CODE));
    }

    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setLoginId(attributes.getValue(GenericConstants.LOGIN));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setSkyNum(attributes.getValue(GenericConstants.SKYNUM));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setMaxAliases(attributes.getValue(GenericConstants.MAXALIASES));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setCreationDate(attributes.getValue(GenericConstants.CREATION_DATE));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setType(attributes.getValue(GenericConstants.TYPE));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setStatus(attributes.getValue(GenericConstants.STATUS));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setMnemonic(attributes.getValue(GenericConstants.MNEMONIC));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setRateLimit(attributes.getValue(GenericConstants.RATELIMIT));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.SETTINGS)) {
        //this.searchRaidusBean.getBitval().add(attributes.getValue(GenericConstants.BITVAL));
        System.out.println(attributes);
        //stuck here
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ALIASES)) {
        this.tempKey = attributes.getValue(GenericConstants.MNEMONIC);
    }
}



public void endElement(String str1, String str2, String element) throws SAXException {
    if (element.equalsIgnoreCase(GenericConstants.RESULT)) {
        this.searchRaidusBean.setResultMessage(this.tempValue);
    }
    if (element.equalsIgnoreCase(GenericConstants.ALIASES)) {
        if (!StringUtils.isBlank(this.tempKey)) {
            this.searchRaidusBean.getAlias().put(this.tempKey, this.tempValue);
        }
    }
}


public void characters(char[] charArray, int i, int j) throws SAXException {
    this.tempValue = new String(charArray, i, j);
}
4

1 に答える 1

3

DefaultHandlerを使用している場合は、イベントを受信しstartElementます。

このメソッドは、そのパラメーターの1つとして属性を保持します。

名前付き属性のインデックスを取得するにはgetIndex(String)を使用し、その属性の値を取得するにはgetValue(int)を使用する必要があります。

Nambariが指摘しているように、インターネット上には何百ものチュートリアルがあり、SOに関する主題に関する投稿がいくつかあります(私は週末に1つ答えました)。

更新しました

私はそれがこのように見えるべきであることを提案します(私はそれをテストしていません)

public void startElement(String uri, String localName, String elementName, Attributes attributes) throws SAXException {

    if (elementName.equalsIgnoreCase(GenericConstants.INFO)) {
        this.searchRaidusBean.setServer(attributes.getValue(GenericConstants.SERVER));
        this.searchRaidusBean.setFunction(attributes.getValue(GenericConstants.FUNCTION));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.RESULT)) {
        this.searchRaidusBean.setResultCode(attributes.getValue(GenericConstants.CODE));
    }

    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setLoginId(attributes.getValue(GenericConstants.LOGIN));
        this.searchRaidusBean.setSkyNum(attributes.getValue(GenericConstants.SKYNUM));
        this.searchRaidusBean.setMaxAliases(attributes.getValue(GenericConstants.MAXALIASES));
        this.searchRaidusBean.setCreationDate(attributes.getValue(GenericConstants.CREATION_DATE));
        this.searchRaidusBean.setType(attributes.getValue(GenericConstants.TYPE));
        this.searchRaidusBean.setStatus(attributes.getValue(GenericConstants.STATUS));
        this.searchRaidusBean.setMnemonic(attributes.getValue(GenericConstants.MNEMONIC));
        this.searchRaidusBean.setRateLimit(attributes.getValue(GenericConstants.RATELIMIT));
    }

    if (elementName.equalsIgnoreCase(GenericConstants.SETTINGS)) {

        for (int index = 0; index < attributes.getLength(); index++) {

            String attName = attributes.getLocalName(index);
            String value = attributes.getValue(index);

            map.put(attName, value);

        }

    }

    if (elementName.equalsIgnoreCase(GenericConstants.ALIASES)) {
        this.tempKey = attributes.getValue(GenericConstants.MNEMONIC);
    }

}

テストされた例で更新

(OPから)データを取得し、次のハンドラーで実行します

DefaultHandler handler = new DefaultHandler() {
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

        if (qName.equalsIgnoreCase("settings")) {

            System.out.println("Parse settings attributes...");

            for (int index = 0; index < attributes.getLength(); index++) {

                String aln = attributes.getLocalName(index);
                String value = attributes.getValue(index);

                System.out.println("    " + aln + " = " + value);


            }

        }

    }
};

そして、私は次の出力を得ました

Parse settings attributes...
    bitval = 4
    status = open
Parse settings attributes...
    bitval = 8192
    status = open
    session_timeout = 10800
Parse settings attributes...
    bitval = 32768
    status = open
    cisco_address_pool = thepool

だから私はあなたが何をしているのか分かりません。

于 2012-09-03T06:10:05.987 に答える