How to get the attribute value in the SAX parser

自古美人都是妖i 提交于 2019-12-10 12:25:55

问题


I have use the SAXParser to read the xml format. Then the startelement method run but I have no idea how to get the url in the method. I dont know what is the solution. Thank you

<enclosure length="1234567" type="image/jpeg" url="http://www.nasa.gov/images/content/664322main_31_lands_cropped_516-387.jpg"/> 

回答1:


What language? What implementation? It is always a good idea to at least tag which language you are trying to implement in.

The idea is to iterate through the attributes parameter in the startElement() function/method:

A Java solution:

public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
{
   int len = atts.getLength();
   // Loop through all attributes and save them as needed
   for(int i = 0; i < len; i++)
   {
      String sAttrName = atts.getLocalName(i);
      if(sAttrName.compareTo("URL") == 0) 
      {
          String sVal = atts.getValue(i);
          // Do something with the *.jpg
      }
   }
}

A C++ Xercesc solution:

void MyClass::startElement(const XMLCh *const uri, const XMLCh *const localname,
                                  const XMLCh *const qname, const Attributes &attributes)
{
    // Loop through all attributes and save them as needed
    XMLSize_t len = attributes.getLength();
    for(XMLSize_t index = 0; index < len; index++)
    {
         CString cAttrName(attributes.getLocalName(index));
         if(cAttrName.Compare(L"URL") == 0)
         {
              CString cVal(attributes.getValue(index));
              // Do something with the *.jpg
         }

    }
}



回答2:


The attributes are passed into the startElement method:

for (int i = 0; i < attrs.getLength(); i++) {
    String attributeName = attrs.getLocalName(i);
    String attributeValue = attrs.getValue(i);
    System.out.println("found attribute with localname=" + attributeName 
    + " and value=" + attributeValue");
}



回答3:


here's my attempt at the SAX ContentHandler; it returns attributes and key / value pairs.

private String mCurrentEl = null;

/** on each element: start */
public void startElement (String uri, String localName, String qName, Attributes attrs) throws SAXException {

    /** keeping a reference */
    mCurrentEl = localName;

    /** each attribute */
    for (int i=0; i < attrs.getLength(); i++) {
        String name  = attrs.getLocalName(i);
        String value = attrs.getValue(i);
        System.out.println("attribute \"" + name + "\" has value \"" + value + "\".");
   }

}

/** on each element: data */
public void characters(char[] chars, int start, int length) throws SAXException {
    String data = new String(chars, start, length).trim();
    if(! data.equals("") && mCurrentEl != null) {
        System.out.println(mCurrentEl + ": " + data);
    }
}

/** on each element: end */
public void endElement(String uri, String localName, String qName) throws SAXException {
    mCurrentEl = null;
}


来源:https://stackoverflow.com/questions/11287915/how-to-get-the-attribute-value-in-the-sax-parser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!