Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用SAX解析器解析Xml_Java_Xml_Xml Parsing_Sax - Fatal编程技术网

Java 用SAX解析器解析Xml

Java 用SAX解析器解析Xml,java,xml,xml-parsing,sax,Java,Xml,Xml Parsing,Sax,我正在尝试使用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 nu

我正在尝试使用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>
如果您正在使用,则您将收到一个
startElement
事件

此方法将数据作为其参数之一

您需要使用来获取命名属性的索引和所述属性的值

正如南巴里所指出的,互联网上有数百篇教程,关于这个主题的帖子也不止一篇(我在周末回答了一篇)

已更新

我建议它应该像这样(我还没有测试过)


所以我不知道你在做什么。

有很多关于SAX解析的教程。所以,这不是一个你仅仅通过发布问题就可以得到一个程序的地方。在发布帮助之前,您需要展示您的努力。此外,在将来,您尝试过的一些代码示例将非常有用,因此我们不会浪费彼此的时间寻找错误的代码路径,除非您确实需要使用SAX(例如,因为家庭作业分配或过时的java版本)拉式解析器会更好、更容易。或者SAX解析需求,或者您打开更好的东西,将代码添加到第一篇文章中。我看过网络教程和谷歌搜索。这个特殊的情况没有讨论,但是上面的for循环获取所有属性(包括从父节点级联的属性)。因此,它将包含诸如login、sknum等帐户属性。它只是我还是SAX解析代码看起来像受虐狂?@vtd xml作者SAX使用访问者模式,不是我首选的解析方法xml@vtd-xml作者否,转到Json
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);
}
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);
    }

}
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