Parse XML using libxml2










1















I am trying from almost a day to parse the below XML using libxml2 cpp library. I need to 2 things:



1) I need to get the list of all "Bucket" tag in a std::vector array.



2) Find if "NextMarker" tag is present or not.



Their are 2 possible XML that can be given as input.



XML 1



<?xml version="1.0" encoding="utf-8"?> 
<EnumerationResults ServiceEndpoint="https://myaccount.blob.core.windows.net/">
<MaxResults>3</MaxResults>
<Containers>
<Bucket>
<Name>audio</Name>
<Properties>
<Last-Modified>Wed, 26 Oct 2016 20:39:39 GMT</Last-Modified>
<Etag>0x8CACB9BD7C6B1B2</Etag>
<PublicAccess>container</PublicAccess>
</Properties>
</Bucket>
<Bucket>
<Name>images</Name>
<Properties>
<Last-Modified>Wed, 26 Oct 2016 20:39:39 GMT</Last-Modified>
<Etag>0x8CACB9BD7C1EEEC</Etag>
</Properties>
</Bucket>
<Bucket>
<Name>textfiles</Name>
<Properties>
<Last-Modified>Wed, 26 Oct 2016 20:39:39 GMT</Last-Modified>
<Etag>0x8CACB9BD7BACAC3</Etag>
</Properties>
</Bucket>
</Containers>
<NextMarker>video</NextMarker>
</EnumerationResults>


XML 2



<EnumerationResults>
<Containers />
<NextMarkerr>/pdtcava01a/container1</NextMarker>
</EnumerationReslts>


My poor code:



int main()
// Read the file
xmlDocPtr doc; /* the resulting document tree */
doc = xmlReadFile("/home/cbs/hemant/azure_d_patch/backend_trim/file_xml.xml", NULL, 0);

// Thing#2. Check if NextMarker tag is present. WORKING
xmlNode *root = xmlDocGetRootElement(doc);
xmlNodePtr lastChild = root->last->prev;
xmlChar *NextMarker = xmlCharStrdup("NextMarker");
if(!xmlStrcmp(lastChild->name, NextMarker))
cout<<"NextMarker tag present"<<endl;


// thing#1. Failing to get the list of Buckets. NOT WORKING
xmlChar *toFind = xmlCharStrdup("Containers");
xmlNodePtr container = findNodeByName(root->children, toFind);
container = container->children;

XmlVal hem(container->children);
std::vector<XmlVal> cont_list = hem.list(); // list() was my internal function which I now scraped.

for (std::vector<XmlVal>::iterator it = cont_list.begin(); it != cont_list.end(); ++it)
bucket b;
b.name_ = (*it)["Name"].content();
b.creation_time_ = (*it)["Last-Modified"].content();
cout<<b.name_<<" "<<b.creation_time_<<endl;
bucket_list.push_back(b);

return 1;











share|improve this question



















  • 1





    You forgot to ask a question... What is happening? What did you expect to happen?

    – Thomas
    Nov 14 '18 at 13:40











  • @Thomas.....Hey thanks I edited the que. Somehow I am not getting the libxml2 right. Frankly speaking I didn't understand its documentation and complexity.

    – Hemant Yadav
    Nov 14 '18 at 13:42







  • 1





    @HemantYadav You haven't asked a question or described a problem, you've only shared your goals. What output are you getting? What output are you expecting? How do they differ? Are you getting errors? If you do, what kind and what do they say? Take a moment to read How do I ask a good question?.

    – François Andrieux
    Nov 14 '18 at 13:56












  • See the line number 8. I was expecting the lastChild->name to return name of tag, but it always returns "text" as output. So I am stuck at initial stage only.

    – Hemant Yadav
    Nov 14 '18 at 14:25











  • It worked, I am able to check the whether NextMarker is present or not. Code need be changed to xmlNodePtr lastChild = root->last->prev; But why root->last->prev when last child is "NextMarker" tag. @François Andrieux @ Thomas

    – Hemant Yadav
    Nov 14 '18 at 14:35
















1















I am trying from almost a day to parse the below XML using libxml2 cpp library. I need to 2 things:



1) I need to get the list of all "Bucket" tag in a std::vector array.



2) Find if "NextMarker" tag is present or not.



Their are 2 possible XML that can be given as input.



XML 1



<?xml version="1.0" encoding="utf-8"?> 
<EnumerationResults ServiceEndpoint="https://myaccount.blob.core.windows.net/">
<MaxResults>3</MaxResults>
<Containers>
<Bucket>
<Name>audio</Name>
<Properties>
<Last-Modified>Wed, 26 Oct 2016 20:39:39 GMT</Last-Modified>
<Etag>0x8CACB9BD7C6B1B2</Etag>
<PublicAccess>container</PublicAccess>
</Properties>
</Bucket>
<Bucket>
<Name>images</Name>
<Properties>
<Last-Modified>Wed, 26 Oct 2016 20:39:39 GMT</Last-Modified>
<Etag>0x8CACB9BD7C1EEEC</Etag>
</Properties>
</Bucket>
<Bucket>
<Name>textfiles</Name>
<Properties>
<Last-Modified>Wed, 26 Oct 2016 20:39:39 GMT</Last-Modified>
<Etag>0x8CACB9BD7BACAC3</Etag>
</Properties>
</Bucket>
</Containers>
<NextMarker>video</NextMarker>
</EnumerationResults>


XML 2



<EnumerationResults>
<Containers />
<NextMarkerr>/pdtcava01a/container1</NextMarker>
</EnumerationReslts>


My poor code:



int main()
// Read the file
xmlDocPtr doc; /* the resulting document tree */
doc = xmlReadFile("/home/cbs/hemant/azure_d_patch/backend_trim/file_xml.xml", NULL, 0);

// Thing#2. Check if NextMarker tag is present. WORKING
xmlNode *root = xmlDocGetRootElement(doc);
xmlNodePtr lastChild = root->last->prev;
xmlChar *NextMarker = xmlCharStrdup("NextMarker");
if(!xmlStrcmp(lastChild->name, NextMarker))
cout<<"NextMarker tag present"<<endl;


// thing#1. Failing to get the list of Buckets. NOT WORKING
xmlChar *toFind = xmlCharStrdup("Containers");
xmlNodePtr container = findNodeByName(root->children, toFind);
container = container->children;

XmlVal hem(container->children);
std::vector<XmlVal> cont_list = hem.list(); // list() was my internal function which I now scraped.

for (std::vector<XmlVal>::iterator it = cont_list.begin(); it != cont_list.end(); ++it)
bucket b;
b.name_ = (*it)["Name"].content();
b.creation_time_ = (*it)["Last-Modified"].content();
cout<<b.name_<<" "<<b.creation_time_<<endl;
bucket_list.push_back(b);

return 1;











share|improve this question



















  • 1





    You forgot to ask a question... What is happening? What did you expect to happen?

    – Thomas
    Nov 14 '18 at 13:40











  • @Thomas.....Hey thanks I edited the que. Somehow I am not getting the libxml2 right. Frankly speaking I didn't understand its documentation and complexity.

    – Hemant Yadav
    Nov 14 '18 at 13:42







  • 1





    @HemantYadav You haven't asked a question or described a problem, you've only shared your goals. What output are you getting? What output are you expecting? How do they differ? Are you getting errors? If you do, what kind and what do they say? Take a moment to read How do I ask a good question?.

    – François Andrieux
    Nov 14 '18 at 13:56












  • See the line number 8. I was expecting the lastChild->name to return name of tag, but it always returns "text" as output. So I am stuck at initial stage only.

    – Hemant Yadav
    Nov 14 '18 at 14:25











  • It worked, I am able to check the whether NextMarker is present or not. Code need be changed to xmlNodePtr lastChild = root->last->prev; But why root->last->prev when last child is "NextMarker" tag. @François Andrieux @ Thomas

    – Hemant Yadav
    Nov 14 '18 at 14:35














1












1








1


1






I am trying from almost a day to parse the below XML using libxml2 cpp library. I need to 2 things:



1) I need to get the list of all "Bucket" tag in a std::vector array.



2) Find if "NextMarker" tag is present or not.



Their are 2 possible XML that can be given as input.



XML 1



<?xml version="1.0" encoding="utf-8"?> 
<EnumerationResults ServiceEndpoint="https://myaccount.blob.core.windows.net/">
<MaxResults>3</MaxResults>
<Containers>
<Bucket>
<Name>audio</Name>
<Properties>
<Last-Modified>Wed, 26 Oct 2016 20:39:39 GMT</Last-Modified>
<Etag>0x8CACB9BD7C6B1B2</Etag>
<PublicAccess>container</PublicAccess>
</Properties>
</Bucket>
<Bucket>
<Name>images</Name>
<Properties>
<Last-Modified>Wed, 26 Oct 2016 20:39:39 GMT</Last-Modified>
<Etag>0x8CACB9BD7C1EEEC</Etag>
</Properties>
</Bucket>
<Bucket>
<Name>textfiles</Name>
<Properties>
<Last-Modified>Wed, 26 Oct 2016 20:39:39 GMT</Last-Modified>
<Etag>0x8CACB9BD7BACAC3</Etag>
</Properties>
</Bucket>
</Containers>
<NextMarker>video</NextMarker>
</EnumerationResults>


XML 2



<EnumerationResults>
<Containers />
<NextMarkerr>/pdtcava01a/container1</NextMarker>
</EnumerationReslts>


My poor code:



int main()
// Read the file
xmlDocPtr doc; /* the resulting document tree */
doc = xmlReadFile("/home/cbs/hemant/azure_d_patch/backend_trim/file_xml.xml", NULL, 0);

// Thing#2. Check if NextMarker tag is present. WORKING
xmlNode *root = xmlDocGetRootElement(doc);
xmlNodePtr lastChild = root->last->prev;
xmlChar *NextMarker = xmlCharStrdup("NextMarker");
if(!xmlStrcmp(lastChild->name, NextMarker))
cout<<"NextMarker tag present"<<endl;


// thing#1. Failing to get the list of Buckets. NOT WORKING
xmlChar *toFind = xmlCharStrdup("Containers");
xmlNodePtr container = findNodeByName(root->children, toFind);
container = container->children;

XmlVal hem(container->children);
std::vector<XmlVal> cont_list = hem.list(); // list() was my internal function which I now scraped.

for (std::vector<XmlVal>::iterator it = cont_list.begin(); it != cont_list.end(); ++it)
bucket b;
b.name_ = (*it)["Name"].content();
b.creation_time_ = (*it)["Last-Modified"].content();
cout<<b.name_<<" "<<b.creation_time_<<endl;
bucket_list.push_back(b);

return 1;











share|improve this question
















I am trying from almost a day to parse the below XML using libxml2 cpp library. I need to 2 things:



1) I need to get the list of all "Bucket" tag in a std::vector array.



2) Find if "NextMarker" tag is present or not.



Their are 2 possible XML that can be given as input.



XML 1



<?xml version="1.0" encoding="utf-8"?> 
<EnumerationResults ServiceEndpoint="https://myaccount.blob.core.windows.net/">
<MaxResults>3</MaxResults>
<Containers>
<Bucket>
<Name>audio</Name>
<Properties>
<Last-Modified>Wed, 26 Oct 2016 20:39:39 GMT</Last-Modified>
<Etag>0x8CACB9BD7C6B1B2</Etag>
<PublicAccess>container</PublicAccess>
</Properties>
</Bucket>
<Bucket>
<Name>images</Name>
<Properties>
<Last-Modified>Wed, 26 Oct 2016 20:39:39 GMT</Last-Modified>
<Etag>0x8CACB9BD7C1EEEC</Etag>
</Properties>
</Bucket>
<Bucket>
<Name>textfiles</Name>
<Properties>
<Last-Modified>Wed, 26 Oct 2016 20:39:39 GMT</Last-Modified>
<Etag>0x8CACB9BD7BACAC3</Etag>
</Properties>
</Bucket>
</Containers>
<NextMarker>video</NextMarker>
</EnumerationResults>


XML 2



<EnumerationResults>
<Containers />
<NextMarkerr>/pdtcava01a/container1</NextMarker>
</EnumerationReslts>


My poor code:



int main()
// Read the file
xmlDocPtr doc; /* the resulting document tree */
doc = xmlReadFile("/home/cbs/hemant/azure_d_patch/backend_trim/file_xml.xml", NULL, 0);

// Thing#2. Check if NextMarker tag is present. WORKING
xmlNode *root = xmlDocGetRootElement(doc);
xmlNodePtr lastChild = root->last->prev;
xmlChar *NextMarker = xmlCharStrdup("NextMarker");
if(!xmlStrcmp(lastChild->name, NextMarker))
cout<<"NextMarker tag present"<<endl;


// thing#1. Failing to get the list of Buckets. NOT WORKING
xmlChar *toFind = xmlCharStrdup("Containers");
xmlNodePtr container = findNodeByName(root->children, toFind);
container = container->children;

XmlVal hem(container->children);
std::vector<XmlVal> cont_list = hem.list(); // list() was my internal function which I now scraped.

for (std::vector<XmlVal>::iterator it = cont_list.begin(); it != cont_list.end(); ++it)
bucket b;
b.name_ = (*it)["Name"].content();
b.creation_time_ = (*it)["Last-Modified"].content();
cout<<b.name_<<" "<<b.creation_time_<<endl;
bucket_list.push_back(b);

return 1;








c++ xml parsing libxml2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 14:53







Hemant Yadav

















asked Nov 14 '18 at 13:38









Hemant YadavHemant Yadav

5210




5210







  • 1





    You forgot to ask a question... What is happening? What did you expect to happen?

    – Thomas
    Nov 14 '18 at 13:40











  • @Thomas.....Hey thanks I edited the que. Somehow I am not getting the libxml2 right. Frankly speaking I didn't understand its documentation and complexity.

    – Hemant Yadav
    Nov 14 '18 at 13:42







  • 1





    @HemantYadav You haven't asked a question or described a problem, you've only shared your goals. What output are you getting? What output are you expecting? How do they differ? Are you getting errors? If you do, what kind and what do they say? Take a moment to read How do I ask a good question?.

    – François Andrieux
    Nov 14 '18 at 13:56












  • See the line number 8. I was expecting the lastChild->name to return name of tag, but it always returns "text" as output. So I am stuck at initial stage only.

    – Hemant Yadav
    Nov 14 '18 at 14:25











  • It worked, I am able to check the whether NextMarker is present or not. Code need be changed to xmlNodePtr lastChild = root->last->prev; But why root->last->prev when last child is "NextMarker" tag. @François Andrieux @ Thomas

    – Hemant Yadav
    Nov 14 '18 at 14:35













  • 1





    You forgot to ask a question... What is happening? What did you expect to happen?

    – Thomas
    Nov 14 '18 at 13:40











  • @Thomas.....Hey thanks I edited the que. Somehow I am not getting the libxml2 right. Frankly speaking I didn't understand its documentation and complexity.

    – Hemant Yadav
    Nov 14 '18 at 13:42







  • 1





    @HemantYadav You haven't asked a question or described a problem, you've only shared your goals. What output are you getting? What output are you expecting? How do they differ? Are you getting errors? If you do, what kind and what do they say? Take a moment to read How do I ask a good question?.

    – François Andrieux
    Nov 14 '18 at 13:56












  • See the line number 8. I was expecting the lastChild->name to return name of tag, but it always returns "text" as output. So I am stuck at initial stage only.

    – Hemant Yadav
    Nov 14 '18 at 14:25











  • It worked, I am able to check the whether NextMarker is present or not. Code need be changed to xmlNodePtr lastChild = root->last->prev; But why root->last->prev when last child is "NextMarker" tag. @François Andrieux @ Thomas

    – Hemant Yadav
    Nov 14 '18 at 14:35








1




1





You forgot to ask a question... What is happening? What did you expect to happen?

– Thomas
Nov 14 '18 at 13:40





You forgot to ask a question... What is happening? What did you expect to happen?

– Thomas
Nov 14 '18 at 13:40













@Thomas.....Hey thanks I edited the que. Somehow I am not getting the libxml2 right. Frankly speaking I didn't understand its documentation and complexity.

– Hemant Yadav
Nov 14 '18 at 13:42






@Thomas.....Hey thanks I edited the que. Somehow I am not getting the libxml2 right. Frankly speaking I didn't understand its documentation and complexity.

– Hemant Yadav
Nov 14 '18 at 13:42





1




1





@HemantYadav You haven't asked a question or described a problem, you've only shared your goals. What output are you getting? What output are you expecting? How do they differ? Are you getting errors? If you do, what kind and what do they say? Take a moment to read How do I ask a good question?.

– François Andrieux
Nov 14 '18 at 13:56






@HemantYadav You haven't asked a question or described a problem, you've only shared your goals. What output are you getting? What output are you expecting? How do they differ? Are you getting errors? If you do, what kind and what do they say? Take a moment to read How do I ask a good question?.

– François Andrieux
Nov 14 '18 at 13:56














See the line number 8. I was expecting the lastChild->name to return name of tag, but it always returns "text" as output. So I am stuck at initial stage only.

– Hemant Yadav
Nov 14 '18 at 14:25





See the line number 8. I was expecting the lastChild->name to return name of tag, but it always returns "text" as output. So I am stuck at initial stage only.

– Hemant Yadav
Nov 14 '18 at 14:25













It worked, I am able to check the whether NextMarker is present or not. Code need be changed to xmlNodePtr lastChild = root->last->prev; But why root->last->prev when last child is "NextMarker" tag. @François Andrieux @ Thomas

– Hemant Yadav
Nov 14 '18 at 14:35






It worked, I am able to check the whether NextMarker is present or not. Code need be changed to xmlNodePtr lastChild = root->last->prev; But why root->last->prev when last child is "NextMarker" tag. @François Andrieux @ Thomas

– Hemant Yadav
Nov 14 '18 at 14:35













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301564%2fparse-xml-using-libxml2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301564%2fparse-xml-using-libxml2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin