【问题标题】:Android: How to parse same tags in SAX Parser?Android:如何在 SAX Parser 中解析相同的标签?
【发布时间】:2012-07-06 22:22:11
【问题描述】:

我正在尝试从 Web 服务解析和获取值。问题是 Web 服务具有相同的元素名称标签,这些标签正在创建解析问题,因为我想获取它们的值但它们具有相同的名称所以我很难将它们称为 localname。

 <countryBean>
        <id>236</id>
        <name>United State</name>
    </countryBean>
    <secutiryQuestion1>
        <id>2</id>
        <question>What was your dream job as a child?</question>
    </secutiryQuestion1>
    <secutiryQuestion2>
        <id>4</id>
        <question>What is the name, breed, and color of your pet?</question>
    </secutiryQuestion2>
    <stateBean>
        <country>236</country>
        <id>5</id>
        <name>California</name>
    </stateBean>
    <statusBean>
        <id>1</id>
        <name>Active</name>
    </statusBean>

我想在不同的变量中获取 ID 标签和其他相邻标签(如名称、问题)的值。这里有 5 个 Id 标签。

我的班级代码是这样的

public class XmlParser extends DefaultHandler {

        public RestfullResponse tempResponse = null;
        String currentValue = null;
        int ServiceType =0;

        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            if(localName.equalsIgnoreCase("countryBean")){
                tempResponse=new RestfullResponse();

                }           

        }


        /* (non-Javadoc)
         * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
         */
        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            if(ServiceType == 1)
            {
                if(localName.equalsIgnoreCase("id")){
                    tempResponse.setCountryId(currentValue);
                }
                if(localName.equalsIgnoreCase("name")){
                    tempResponse.setCountryName(currentValue);
                }

                if(localName.equalsIgnoreCase("id")){
                    tempResponse.setStateId(currentValue);
                }
                if(localName.equalsIgnoreCase("question")){
                    tempResponse.setstateBean(currentValue);
                }

                if(localName.equalsIgnoreCase("Id")){
                    tempResponse.setStatusId(currentValue);
                }
                if(localName.equalsIgnoreCase("requestSuccess")){
                    tempResponse.setStatus(currentValue);
                }
            }
        }

        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            currentValue = new String(ch, start, length);
        }

        public RestfullResponse getResponse(){
            return tempResponse;
        }
}

请帮忙把它们存放在

String CountryName= "";
String CountryId = "";
String Statename = "";
String StateId ="";
String Status = "";
String StatusId = "";
String Username ="";
String SecurityQuestion = "";
String SecurityQuestionId = "";

【问题讨论】:

    标签: android web-services parsing xml-parsing saxparser


    【解决方案1】:

    添加布尔变量isCountryBean;

    boolean isCountryBean = false , isSecurityQuestion1 = false;
    

    然后在 startElement(....) 中写:

    if(localName.equalsIgnoreCase("countryBean")){
        isCountryBean = true;
        // other stuff
    } else if (localName.equalsIgnoreCase("secutiryQuestion1")){
        isSecurityQuestion1 = true;
        isCountryBean = false;
    }
    

    在 endElement(...) 中检查:

    if(localName.equalsIgnoreCase("id")){
        if(isCountryBean){
            tempResponse.setCountryId(currentValue); 
         }
    }
    if(localName.equalsIgnoreCase("name")){
        if(isCountryBean){
            isCountryBean = false;
            tempResponse.setCountryName(currentValue);
    
         }
    }
    

    为其他最外向的标签做这件事。希望对你有帮助。

    【讨论】:

      【解决方案2】:

      您需要维护stack of root nodes

      获取String root 类型的变量并维护stack of root element

      startElement 方法内部:

      public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException,
                  IllegalArgumentException
      {
      
         // call to abstract method
         onStartElement(namespaceURI, localName, qName, atts);
      
         // Keep the trace of the root nodes
         if (root.length() > 0)
         {
          root= root.concat(",");
         }
         root= root.concat(localName);
      }
      

      endElement 方法内部:

      public void endElement(String namespaceURI, String localName, String qName) throws SAXException
      {
         // remove the end element from stack of root elements
         int index = root.lastIndexOf(',');
         if (index > 0)
         {
          root= root.substring(0, index);
         }
         onEndElement(namespaceURI, localName, qName);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-17
        • 2011-12-05
        • 2013-08-29
        • 1970-01-01
        • 2012-05-26
        相关资源
        最近更新 更多