Pregunta

Tengo un archivo XML como se muestra a continuación:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<RULES>
    <RULE DESCRIPTION="" DOMAIN="CAE" NAME="Filter1 " TYPE="FILTER">
        <ATTRS>
            <ATTR RULE_NAME="myFilter1" />
        </ATTRS>
    </RULE>

    <RULE DESCRIPTION="" DOMAIN="CAE" NAME="Skip1" TYPE="SKIP">
        <ATTRS>
            <ATTR RULE_NAME="mySkip1" />
        </ATTRS>
    </RULE>

    <RULE DESCRIPTION="" DOMAIN="CAE" NAME="Filter_new " TYPE="FILTER">
        <ATTRS>
            <ATTR RULE_NAME="myFilter1" />
        </ATTRS>
    </RULE>

    <RULE DESCRIPTION="" DOMAIN="CAE" NAME="Skip_new" TYPE="SKIP">
        <ATTRS>
            <ATTR RULE_NAME="mySkip1" />
        </ATTRS>
    </RULE>
</RULES>

¿Cómo puedo analizar este archivo usando saxParser (en c ++) de modo que puedo extraer el valor del atributo reglas_name solo si el atributo de tipo tiene un valor "filtro"?

Puedo hacer esto usando DomParser, pero me gustaría usar SaxParser.

¿Fue útil?

Solución

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    if (localName.equals("city")) {
        String city = attributes.getValue("data");
        info.setCity(city);
    } else if (localName.equals("temp_c")) {
        String t = attributes.getValue("data");
        int temp = Integer.parseInt(t); 
        info.setTemp(temp);
    } else if (localName.equals("condition")) {
        String condition = attributes.getValue("data");
        info.setCondition(condition);
    }
}

Puede usar para comparar los atributos como M haciendo "datos", que son los atributos infantiles. espero que esto funcione

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top