senjuhashirama / pugixml

Automatically exported from code.google.com/p/pugixml
0 stars 0 forks source link

Problem to get first child #168

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I am loading the XML string successfully. 
I am trying to get first child in the document. But not getting the desired 
output.
Following code I am using. 

pugi::xml_parse_result  re = doc.load_buffer(&xmldata,sizeof(xmldata));

   if(re.status != pugi::status_ok)
   {
     std::cout<<"Error";
   }
   //If successfully  loaded get first child   
   node = doc.first_child();

   if(NULL == node )
   {
      std::cout<<"Error";
   }
Please provide any additional information below.

Original issue reported on code.google.com by kshitija...@gmail.com on 5 Sep 2012 at 5:37

GoogleCodeExporter commented 9 years ago
There are strings that are parsed without errors but result in empty documents. 
A simple example is an empty string (""), or a string that does not contain any 
tags, just text or whitespace ("abcde").

What is the type of xmldata? Is it "const char*" or "std::string" by any 
chance? If so, the load_buffer call is incorrect - you should provide it with a 
memory buffer represented as a pointer and size - i.e., for std::string source:

std::string data = ...;
doc.load_buffer(&data[0], data.size());

Original comment by arseny.k...@gmail.com on 5 Sep 2012 at 5:44

GoogleCodeExporter commented 9 years ago
Type of xmldata is std::wstring&. Tried with xmldata.size(); But problem is not 
solved.

Original comment by kshitija...@gmail.com on 5 Sep 2012 at 5:56

GoogleCodeExporter commented 9 years ago
For std::wstring&, you usually have to specify that the buffer consists of 
wchar_t like this:

doc.load_buffer(xmldata.c_str(), xmldata.size() * sizeof(wchar_t), 0, 
pugi::encoding_wchar);

If you don't specify encoding_wchar:

doc.load_buffer(xmldata.c_str(), xmldata.size() * sizeof(wchar_t));

Then pugixml will try to guess the encoding - since your data probably does not 
contain BOM, successful encoding detection is not guaranteed (you can check the 
resluts of the encoding detection by printing re.encoding).

Original comment by arseny.k...@gmail.com on 5 Sep 2012 at 5:59

GoogleCodeExporter commented 9 years ago
Thanks. my get first child problem is solved.
Now I have another problem, related to attribute. I am retrieving child node 
from document using my custom class object. this custom class contains 
pugi::xml_node member variable and using custom class object getting the 
attribute of the node.
but when program enters in function for retrieving attribute the pugi::xml_node 
object is garbage. 
please refer the following code

 CXmlNode testnode(*log);
      testdoc.GetChildNode(L"/testnodes/nodecategory2", 1, testnode)

testnode.GetAttributeValue(L"attribute1", attribute_value);
attribute_value == L"valueD";

when code enters in GetAttributeValue() testnode.pugi::xml_node_object is 
garbage.

Original comment by kshitija...@gmail.com on 5 Sep 2012 at 9:51

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I am trying to get owner document of the node. But failed.

Original comment by kshitija...@gmail.com on 5 Sep 2012 at 12:32

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
1. As far as getting the node's document, the only way is to use node.root() - 
this'll give you the xml_node that corresponds to the document. It is currently 
impossible to get the actual xml_document object.

2. I'm not sure what's wrong with the code above and what you mean by garbage. 
Generally, if xml_node object contains an invalid pointer, it means that the 
document was destroyed while the node was still alive. This is a usage error - 
document controls the lifetime of all nodes and attributes. If the document 
still exists at this point, I'll have to see the code for CXmlNode and 
CXmlDocument to be able to guess the problem.

Original comment by arseny.k...@gmail.com on 5 Sep 2012 at 4:12

GoogleCodeExporter commented 9 years ago
If I want to add the new attribute for a particular node and only attribute 
name & node object is given then how to add that attribute in owner document of 
the node?

Original comment by kshitija...@gmail.com on 6 Sep 2012 at 5:38

GoogleCodeExporter commented 9 years ago
Why would you need a document object for that?

xml_node node = ...;
xml_attribute attr = node.append_attribute("attrname");

Original comment by arseny.k...@gmail.com on 6 Sep 2012 at 7:24

GoogleCodeExporter commented 9 years ago

Original comment by arseny.k...@gmail.com on 17 Sep 2012 at 8:33