如何跳过使用 xmlTextReader 关闭 HTML 标记?
How can you skip closing HTML tag with xmlTextReader?
我是新手 to/learning libxml2。每当我找到特定的 HTML 标签时,我都需要采取行动(在下面的简化示例中,该行动是 std::cout
)。我下面的程序在遇到与指定字符串 ("B") 匹配的开始和结束标记时执行此操作 both。但是,我只想在找到开始标签后采取行动。如何才能做到这一点?我无法从 libxml2 文档中 find/understand 是否有区分开始和结束标记的方法,而且我找不到类似的 SO 问题。
代码:
#include <iostream>
#include <libxml/xmlreader.h>
int main( int argc, char* argv[] )
{
int ret;
xmlTextReaderPtr r = xmlNewTextReaderFilename("foo.xml");
if ( !r )
{
return -1;
}
ret = xmlTextReaderRead( r );
while ( 1 == ret )
{
if ( std::string("B") == (const char*)xmlTextReaderConstName( r ) )
{
std::cout << "Found desired tag" << std::endl;
}
ret = xmlTextReaderRead( r );
}
if ( r )
{
xmlFreeTextReader( r );
}
return 0;
}
这样编译:
>g++ --version
g++ (GCC) 4.8.3 20140911 (Red Hat 4.8.3-7)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>g++ -lxml2 -I/usr/include/libxml2 main.cpp
运行 有了这个 XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<A version="02.00.00" priority="0" reliable="false">
<B attr1="Type_B" attr2="usb" attr3="600">
<C/>
<D/>
</B>
</A>
此输出结果:
>./a.out
Found desired tag
Found desired tag
而我希望 "Found desired tag" 只输出一次,即仅在遇到开头 <B>
HTML 标签时输出。
您可以使用 xmlTextReaderNodeType(reader) to determine what "type" of node the reader is currently on, as defined here 或 xmlreader.h 中的 xmlReaderTypes 枚举。
在这种情况下,您需要区分 XML_READER_TYPE_ELEMENT 和 XML_READER_TYPE_END_ELEMENT(忽略后者)。
我是新手 to/learning libxml2。每当我找到特定的 HTML 标签时,我都需要采取行动(在下面的简化示例中,该行动是 std::cout
)。我下面的程序在遇到与指定字符串 ("B") 匹配的开始和结束标记时执行此操作 both。但是,我只想在找到开始标签后采取行动。如何才能做到这一点?我无法从 libxml2 文档中 find/understand 是否有区分开始和结束标记的方法,而且我找不到类似的 SO 问题。
代码:
#include <iostream>
#include <libxml/xmlreader.h>
int main( int argc, char* argv[] )
{
int ret;
xmlTextReaderPtr r = xmlNewTextReaderFilename("foo.xml");
if ( !r )
{
return -1;
}
ret = xmlTextReaderRead( r );
while ( 1 == ret )
{
if ( std::string("B") == (const char*)xmlTextReaderConstName( r ) )
{
std::cout << "Found desired tag" << std::endl;
}
ret = xmlTextReaderRead( r );
}
if ( r )
{
xmlFreeTextReader( r );
}
return 0;
}
这样编译:
>g++ --version
g++ (GCC) 4.8.3 20140911 (Red Hat 4.8.3-7)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>g++ -lxml2 -I/usr/include/libxml2 main.cpp
运行 有了这个 XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<A version="02.00.00" priority="0" reliable="false">
<B attr1="Type_B" attr2="usb" attr3="600">
<C/>
<D/>
</B>
</A>
此输出结果:
>./a.out
Found desired tag
Found desired tag
而我希望 "Found desired tag" 只输出一次,即仅在遇到开头 <B>
HTML 标签时输出。
您可以使用 xmlTextReaderNodeType(reader) to determine what "type" of node the reader is currently on, as defined here 或 xmlreader.h 中的 xmlReaderTypes 枚举。
在这种情况下,您需要区分 XML_READER_TYPE_ELEMENT 和 XML_READER_TYPE_END_ELEMENT(忽略后者)。