How to extract values of inner elements using SAXParser
-
25-10-2019 - |
سؤال
I have a xml file as below:
<?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>
How can I parse this file using SAXParser (in C++) such that I am able to extract the value of RULE_NAME attribute only if the TYPE attribute has a value "FILTER".
I can do this using the DOMParser but I would like to use SAXParser.
المحلول
@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);
}
}
you can use to compair attributes as m doing "data" which is the child attributes. hope this works
لا تنتمي إلى StackOverflow