如何在 SAX 解析器中获取属性值 [英] How to get the attribute value in the SAX parser
本文介绍了如何在 SAX 解析器中获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 SAXParser 来读取 xml 格式.然后 startelement 方法运行,但我不知道如何在方法中获取 url.我不知道解决方案是什么.谢谢
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"/>
推荐答案
什么语言?什么实现?至少标记您要尝试使用的语言总是一个好主意.
What language? What implementation? It is always a good idea to at least tag which language you are trying to implement in.
想法是遍历startElement()函数/方法中的attributes参数:
The idea is to iterate through the attributes parameter in the startElement() function/method:
Java 解决方案:
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
}
}
}
C++ Xercesc 解决方案:
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
}
}
}
这篇关于如何在 SAX 解析器中获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文