免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1699 | 回复: 0
打印 上一主题 下一主题

xerces 解析 XML文件问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-15 15:33 |只看该作者 |倒序浏览
各位大侠 请教个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;
     }
   }

这是现在用的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP