C++ 为什么在C+;中使用tinyXML2获得空指针+;?

C++ 为什么在C+;中使用tinyXML2获得空指针+;?,c++,tinyxml2,C++,Tinyxml2,我是XML解析行业的新手。很抱歉问了这么多愚蠢的问题 我想浏览我的XML: <?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer<

我是XML解析行业的新手。很抱歉问了这么多愚蠢的问题

我想浏览我的XML:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>
   <book id="bk104">
      <author>Corets, Eva</author>
      <title>Oberon's Legacy</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-03-10</publish_date>
      <description>In post-apocalypse England, the mysterious 
      agent known only as Oberon helps to create a new life 
      for the inhabitants of London. Sequel to Maeve 
      Ascendant.</description>
   </book>
   <book id="bk105">
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-09-10</publish_date>
      <description>The two daughters of Maeve, half-sisters, 
      battle one another for control of England. Sequel to 
      Oberon's Legacy.</description>
   </book>
   <book id="bk106">
      <author>Randall, Cynthia</author>
      <title>Lover Birds</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2000-09-02</publish_date>
      <description>When Carla meets Paul at an ornithology 
      conference, tempers fly as feathers get ruffled.</description>
   </book>
   <book id="bk107">
      <author>Thurman, Paula</author>
      <title>Splish Splash</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2000-11-02</publish_date>
      <description>A deep sea diver finds true love twenty 
      thousand leagues beneath the sea.</description>
   </book>
   <book id="bk108">
      <author>Knorr, Stefan</author>
      <title>Creepy Crawlies</title>
      <genre>Horror</genre>
      <price>4.95</price>
      <publish_date>2000-12-06</publish_date>
      <description>An anthology of horror stories about roaches,
      centipedes, scorpions  and other insects.</description>
   </book>
</catalog>
虽然
pRoot1
可以运行,但
pbook1
的值始终为
NULL
。 我希望它指向
bk101

有什么帮助或建议吗?
提前感谢

您正在尝试查找名为
图书id
的元素。没有这样的元素。

但是,有些元素的名称为
book

(每个都有一个名为
id
的属性)

您还需要解析器跳过您的XML声明,该声明目前正被
FirstChild
调用捕获(因此
*pRoot1
不是您想象的那样!)。您可以在那里尝试
FirstChildElement(“catalog”)


另一方面,我建议回顾一下错误处理过程;此时,如果
pRoot1
是一个空指针,那么您将向控制台写入一条消息,然后愉快地继续尝试取消对该空指针的引用。

您正在尝试查找名为
book id
的元素。没有这样的元素。

但是,有些元素的名称为
book

(每个都有一个名为
id
的属性)

您还需要解析器跳过您的XML声明,该声明目前正被
FirstChild
调用捕获(因此
*pRoot1
不是您想象的那样!)。您可以在那里尝试
FirstChildElement(“catalog”)


另一方面,我建议回顾一下错误处理过程;目前,如果
pRoot1
是一个空指针,那么您将向控制台写入一条消息,然后愉快地继续尝试取消对该空指针的引用。

即使您的程序未能加载XML,您也在继续该过程。即使您的程序未能加载XML,您也在继续该过程。您完全正确。我同意错误管理的观点:我只是复制了整个代码的一部分。尽管如此,我如何让pbook1指向第一本书?@Wing:通过编写
book
而不是
book id
。我尝试过:xmlement*pbook1=pRoot1->FirstChildElement(“book id”);xmlement*pbook1=pRoot1->FirstChildElement(“book”);xmlement*pbook1=pRoot1->FirstChildElement();我总是很紧张NULL@Wing:您可能也需要跳过XML声明。检查根节点以查看它实际上是什么。您使用的是哪些文档?@Wing:是的,请注意他们的示例没有XML声明。如果您检查
*pRoot1
,您可能会看到它不被称为
目录
。对根节点也尝试使用
FirstChildElement
(而不是
FirstChild
),你完全正确。我同意错误管理的观点:我只是复制了整个代码的一部分。尽管如此,我如何让pbook1指向第一本书?@Wing:通过编写
book
而不是
book id
。我尝试过:xmlement*pbook1=pRoot1->FirstChildElement(“book id”);xmlement*pbook1=pRoot1->FirstChildElement(“book”);xmlement*pbook1=pRoot1->FirstChildElement();我总是很紧张NULL@Wing:您可能也需要跳过XML声明。检查根节点以查看它实际上是什么。您使用的是哪些文档?@Wing:是的,请注意他们的示例没有XML声明。如果您检查
*pRoot1
,您可能会看到它不被称为
目录
。对根节点也尝试使用
FirstChildElement
(而不是
FirstChild
)。
tinyxml2::XMLDocument mappedxml1;
XMLError eResult = mappedxml1.LoadFile(filename1);
if(eResult == XML_SUCCESS){
        printf("XMLERROR is %d\nXML loading successfull.\n",eResult);
    }
    else{
        printf("XMLERROR is %d\nXML loading unsuccessfull.\n",eResult);
    }
XMLNode * pRoot1 = mappedxml1.FirstChild();
    if(pRoot1 == NULL){printf("ERROR: NO ROOT\n");}
    else{printf("Good pointer\n");}
XMLElement * pbook1 = pRoot1->FirstChildElement("book id");
    if (pbook1 == NULL){printf("ERROR PARSING ELEMENT\n");}
    else{printf("GOT IT\n");}