使用libxml2 sax解析器获取值错误

时间:2012-10-30 13:04:33

标签: c++ sax libxml2

我正在尝试使用libxml2的sax接口解析xml文件。有时它工作得很好,但后来我在xml中更改了2行顺序(它仍然有效),并且在解析后某些值变为无效。我正在为startElement使用startElementNsSAX2Func,它有一个参数const xmlChar **属性,用于存储当前元素的属性。

在我的startElement方法的开头,我创建了一个处理属性的简单对象 以下是该类的代码:

class XMLElementAttributes {
public:
  static const int AttributeArrayWidth = 5;
  static const int LocalNameIndex = 0;
  static const int PrefixIndex = 1;
  static const int URIIndex = 2;
  static const int ValueIndex = 3;
  static const int EndIndex = 4;

  XMLElementAttributes( int nb_attributes, const xmlChar **attributes) :
  nb_attributes(nb_attributes),
  attributes(attributes){
  }

  xmlChar* getLocalName( int index ) const {
    return (xmlChar*)attributes[ AttributeArrayWidth * index + LocalNameIndex];
  }

  xmlChar* getValue( int index ) const{
      return (xmlChar*)std::string(attributes[ AttributeArrayWidth * index + ValueIndex],attributes[ AttributeArrayWidth * index + EndIndex]).c_str(); 
  }

  int getLength() const{
    return nb_attributes;
  }

private:
  int nb_attributes;
  const xmlChar ** attributes;
};

(xmlChar是Typedef unsigned char xmlChar)

然后如果我需要存储一个属性的值,我用这个staic方法克隆它(我也尝试使用libxml2的xmlStrdup,结果是一样的):

xmlChar* cloneXMLString(const xmlChar* const source) {
    xmlChar* result;
    int len=0;
    std::cout<<"source"<<std::endl;
    while (source[len] != '\0'){
        std::cout<<(void*)&source[len] << ": " << source[len] <<std::endl;
        len++;
    }
    std::cout<<std::endl;
    std::cout<<"result, "<<std::endl;
    result = new xmlChar[len+1];
    for (int i=0; i<len; i++){
        result[i] = source[i];
        std::cout<<(void *)&source[i] << ": "<< source[i] << std::endl;
    }
    std::cout<<std::endl;
    result[len] = '\0';
    return result;
}

它的工作率为99%,但有时最后的结果与源不相似。这是一个示例输出(输入是abcdef,\ 0终止):

source
0x7fdb7402cde8: a
0x7fdb7402cde9: b
0x7fdb7402cdea: c
0x7fdb7402cdeb: d
0x7fdb7402cdec: e
0x7fdb7402cded: f


result, 
0x7fdb7402cde8: !
0x7fdb7402cde9: 
0x7fdb7402cdea: 
0x7fdb7402cdeb: 
0x7fdb7402cdec: x
0x7fdb7402cded: 

我这样称呼它:

xmlChar* value = cloneXMLString(attributes.getValue(index));

因此虽然源的地址没有改变,但它的价值确实如此。解析xml文件没有任何问题,克隆后的下一个值再次生效。

如果xml文件未更改,则错误始终位于相同的元素和参数中。如果我在xml中改变一些东西,例如:

<somenodes a="arg1" b="arg2">
  <node c="abc" d="def" />
  <node c="ghi" d="jkl" />
</somenodes>

<somenodes a="arg1" b="arg2">
  <node c="ghi" d="jkl" />
  <node c="abc" d="def" />
</somenodes>

错误出现在其他地方,或者它消失了,解析工作正常。什么可能导致这种情况?

编辑:

我的开始元素方法:

void MyParser::startElement( void * ctx,
        const xmlChar * localName,
        const xmlChar * prefix,
        const xmlChar * URI,
        int nb_namespaces,
        const xmlChar ** namespaces,
        int nb_attributes,
        int nb_defaulted,
        const xmlChar ** attrs ){

    XMLElementAttributes attributes ( nb_attributes, attrs );

    switch ( state ) {
    case Somestate:
       if ( xmlStrcmp( localName, StrN("SomeName").xmlCharForm() ) == 0) {
         someVar = new SomeObject(attributes);
       } 
    break;

    ...

    }
}

StrN从char *创建xmlChar。 someVar是MyParser类中的一个staic字段(startElement也是静态的)。在SomeObject的构造函数中,我尝试获取这样的属性值:

class SomeObject {
    public:
    SomeObject( XMLElementAttributes &attributes){
        for (int i=0; i< attributes.getLength(); i++) {
            xmlChar* name = attributes.getLocalName(i);
            if ( xmlStrcmp( name, StrN("somename").xmlCharForm()) == 0 ) {
                somename = cloneXMLString(attributes.getValue(i));
            }
            ...
        }
    }
};

1 个答案:

答案 0 :(得分:0)

很明显,源并未指向有效内存。这可能是因为内存已经被释放,或者可能是因为它指向已经退出的函数中声明的堆栈内存。

这种记忆可能会以不可预测的方式被覆盖,这就是你在这里看到的。

需要查看更多上下文,特别是如何调用cloneXMLString以及传递给此函数的内存的来源,以获得更详细的答案。