[英]C++: Trouble with Parsing XML using Libxml
I am having a lot of trouble working with the libxml2 library to parse an xml file. 使用libxml2库解析xml文件时遇到很多麻烦。
I have weeded out a previous, similar problem, but have run into another. 我已经清除了先前的类似问题,但遇到了另一个问题。
Here is the problem code: 这是问题代码:
class SSystem{
public:
//Constructors
SSystem(){};
//Make SSystem from XML Definition. Pass ptr to node
SSystem(xmlNodePtr Nptr, xmlDocPtr Dptr){
name = wxString((char *)xmlGetProp(Nptr, (xmlChar*)"name"), wxConvUTF8);
//Move to next level down, the <general> element
Nptr = Nptr->xmlChildrenNode;
//Move one more level down to the <radius> element
Nptr = Nptr->xmlChildrenNode;
//Get Radius value
if (!xmlStrcmp(Nptr->name, (const xmlChar *)"radius")) {
char* contents = (char*)xmlNodeGetContent(Nptr);
std::string test1 = std::string(contents);
radius = wxString(contents, wxConvUTF8);
}
}
Both an xmlNodePtr and an xmlDocPtr are passed to the constructor, which works fine taking just a property ("name"), but is now choking on further parsing. xmlNodePtr和xmlDocPtr都传递给构造函数,该构造函数仅接受属性(“名称”)即可正常工作,但现在在进行进一步的解析时遇到了麻烦。
Here is a piece of the xml file in question: 这是一个有问题的xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<Systems>
<ssys name="Acheron">
<general>
<radius>3500.000000</radius> <-- I am trying to get this value (3500).
<stars>300</stars>
<asteroids>0</asteroids>
<interference>0.000000</interference>
<nebula volatility="0.000000">0.000000</nebula>
</general>
It compiles fine, but crashes when the constructor is loaded (I know because, if I comment out the if conditional and the char* contents = (char*)xmlNodeGetContent(Nptr->xmlChildrenNode), it runs fine. 它可以正常编译,但是在加载构造函数时会崩溃(我知道,因为如果我注释掉if条件和char *内容=(char *)xmlNodeGetContent(Nptr-> xmlChildrenNode),它可以正常运行。
I've tried so many different things (removed one of the Nptr->xmlChildrenNode), but nothing works. 我尝试了许多不同的操作(删除了Nptr-> xmlChildrenNode之一),但是没有任何效果。
What is wrong? 怎么了?
This: 这个:
char* contents = (char*)xmlNodeGetContent(Nptr->xmlChildrenNode)
Should probably be this: 应该可能是这样的:
char* contents = (char*)xmlNodeGetContent(Nptr)
I just wrote a C++ wrapper to libxml2. 我刚刚为libxml2写了一个C ++包装器。 It is on github if someone is interested: https://github.com/filipenf/libxml-cpp-wrapper
如果有人感兴趣,它在github上: https : //github.com/filipenf/libxml-cpp-wrapper
The idea is to make the use of libxml2 easier for C++ programmers - that's the main goal of this wrapper. 这个想法是使C ++程序员更容易使用libxml2-这是此包装器的主要目标。
In the github repository there is a simple example of how to use it, but you can use it like this: string office_phone = reader.getNodes()[0]["Customer"]["ContactInfo"]["OfficePhone"].text;
在github存储库中,有一个简单的用法示例,但您可以像这样使用它:
string office_phone = reader.getNodes()[0]["Customer"]["ContactInfo"]["OfficePhone"].text;
It is a work-in-progress so there is many room for improvement.... 这是一个进行中的工作,因此还有很多改进的余地。
Okay, I am going to use a different XML parsing library, as Libxml is a bit too complicated for me. 好的,我将使用另一个XML解析库,因为Libxml对我来说有点太复杂了。
I am looking into using MiniXML (http://www.minixml.org/). 我正在研究使用MiniXML(http://www.minixml.org/)。
@Biosci3c: The method you are calling returns some fake value. @ Biosci3c:您正在调用的方法返回一些假值。 You should not call the method
您不应该调用该方法
char*)xmlNodeGetContent(Nptr->xmlChildrenNode) char *)xmlNodeGetContent(Nptr-> xmlChildrenNode)
instead you have to get the data corresponding to radius in cdata callback method below here. 相反,您必须在下面的cdata回调方法中获取与radius对应的数据。
void cdataBlock (void * ctx, const xmlChar * value, int len) void cdataBlock(void * ctx,const xmlChar * value,int len)
Check out in libxml library documentation for reference... 查阅libxml库文档以获取参考...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.