- 论坛徽章:
- 0
|
各位大侠 请教个XML解析问题
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
这个文件里的 title,author 我想提取出来这样的名字, 但只能提取出Harry Potter, J K. Rowling
请问我如何史title对应Harry Potter, author对应J K. Rowling, 或者能搞属性也行 比如category对应children 我如何提取 用什么函数
DOMDocument* xmlDoc = m_DOMXmlParser->getDocument();
DOMElement *pRoot = xmlDoc->getDocumentElement();
DOMNodeList *List = pRoot->getChildNodes();
//int len = List->getLength();
// printf("\n list size = %d\n", len);
if (!pRoot )
{
throw(std::runtime_error( "empty XML document" ));
}
// create a walker to visit all text nodes.
/**********************************************
DOMTreeWalker *walker =
xmlDoc->createTreeWalker(pRoot, DOMNodeFilter::SHOW_TEXT, NULL, true);
// use the tree walker to print out the text nodes.
std::cout<< "TreeWalker:\n";
for (DOMNode *current = walker->nextNode(); current != 0; current = walker->nextNode() )
{
char *strValue = XMLString::transcode( current->getNodeValue() );
std::cout <<strValue;
XMLString::release(&strValue);
}
std::cout << std::endl;
*************************************************/
// create an iterator to visit all text nodes.
DOMNodeIterator* iterator = xmlDoc->createNodeIterator(pRoot,
DOMNodeFilter::SHOW_TEXT, NULL, true);
// use the tree walker to print out the text nodes.
//std::cout<< "iterator:\n";
for ( DOMNode * current = iterator->nextNode();
current != 0; current = iterator->nextNode() )
{
if (current->getNodeType() == DOMNode::TEXT_NODE) {
string strValue = XMLString::transcode(current->getNodeValue());
int pos = strValue.find('\n', 0);
if ( pos != string::npos) continue;
char* name = XMLString::transcode(current->getNodeName());
//XMLTan->TranslatorUTF8ToChinese(strValue);
std::cout <<strValue<<" end"<<endl;
}
}
这是现在用的 |
|