Xerces-C 上的 DOM 元素内存分配管理

发布于 2024-10-11 23:50:41 字数 1254 浏览 1 评论 0原文

我已经在 xml 解析问题上苦苦挣扎了两天,但它还没有完成:)

好吧,经过多次尝试,我终于决定在我的 c++ 应用程序中使用 Xerces-C 来解析 xml。

嗯...我尝试了TinyXml、RapidXml 和其他...Xerces 在从内存分配的角度管理节点时也以相同的方式表现。

好吧,DOMElement、DOMDocument、DOMNode 都是私有构造类...我知道这是为了确保解析时的最佳性能,但当尝试管理类之间的节点时,这确实是一件坏事。

我有这个问题。 我想创建一个存储 xml 文档的类(一个 DOM 对象,它具有我想要的所有节点并且可以自由导航,所以它不是字符串 var,而是 DOM obj,我认为 DOMElement/DOMDocument 很好)。嗯,这个对象是我班级的成员。在我的类中使用函数,我想让这棵 DOM 树及时增长...在内部,我保留这棵树,当我想要时,我将其打印到文件中。 这就是我想做的。我知道这看起来很奇怪,但这是我的要求。我必须这样做,不考虑其他所有解决方案......抱歉。 (我应该告诉你为什么要这样做,但这需要很多时间)。

用代码表达这一点,以下几行是我想要实现的目标的表示:

class MyTree {
public:
    MyTree();
    ^MyTree(); // It is a tilde :)
    // Methods
    void AddToTree(std::string el); // Adds a new node somewhere in the DOM
    std::string GetTree(); // Inserts the tree in a string
private:
    DOMDocument _xmldoc;
};

考虑到私有构造函数引起的所有限制,并且我只能从 XercesDOMParser 获取 DOMDocument,并且获得指针,我怎样才能实现这一点... 如果在构造函数中我这样做:

MyTree::MyTree() {
    XercesDOMParser parser;
    parser.parse(XMLString::transcode("<...>...</...>"));
    this->_xmldoc = parser.getDocument();
}

在将文档分配给 _xmldoc 后,构造函数超出范围并且解析器被删除,因此它拥有的每个组件,所以我的 _xmldoc 将指向任何内容...

我该如何解决这个问题问题???

谢谢...

It has been two days for me to struggle on xml parsing problems and it still hasn't finished yet :)

Well, After many trils I finally decided to use Xerces-C to parse xml in my c++ application.

Well... I tried TinyXml, RapidXml and others.... Xerces too behave in the same way when managing nodes on the point of view of memory allocation.

Well, DOMElement, DOMDocument, DOMNode tey all are private construct classes... I know it is for ensuring best performance when parsing but it gets really a bad thing when trying to manage nodes between classes.

I have this problem.
I would like to create a class that stores an xml document (a DOM object that has all nodes I want and that I can navigate freely, so it is not a string var, but a DOM obj, I suppose a DOMElement/DOMDocument is good). Well, this object is a member of my class. Using functions in my class I want to let this DOM Tree grow in time... Internally I keep the tree and when I want i print it to a file.
This is what I want to do. I know that it seems weird but this is my requirement. I must do so, every other solution is not to be considered... sorry. (I should tell you the reason why this is the implementation, but it would take much time).

Expressing this in code, the following lines are the representation of what I would like to achieve:

class MyTree {
public:
    MyTree();
    ^MyTree(); // It is a tilde :)
    // Methods
    void AddToTree(std::string el); // Adds a new node somewhere in the DOM
    std::string GetTree(); // Inserts the tree in a string
private:
    DOMDocument _xmldoc;
};

Considering all restrictions caused by private constructors, and that I can get a DOMDocument only from a XercesDOMParser, and I get the pointer, how can I achieve this...
If in the constructor I do so:

MyTree::MyTree() {
    XercesDOMParser parser;
    parser.parse(XMLString::transcode("<...>...</...>"));
    this->_xmldoc = parser.getDocument();
}

After assigning to _xmldoc the document, the constructor function goes out of scope and parser is deleted, and so every component owned by it, so my _xmldoc will point to nothing...

How can I solve this problem???

Thank you...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2024-10-18 23:50:41

您需要保留解析器。可以将其作为参数传递(通过引用),使其成为 MyTree 的成员(再次作为引用),或者将其放入单例类中。

(顺便说一句,_xmldoc 应该是一个指针。)

You need to keep the parser around. Either pass it as an argument (by reference), make it a member of MyTree (again, as a reference), or put it in a singleton class.

(_xmldoc should be a pointer, btw.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文