Convert xmlChar to int

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 11
Reputation: fedderico10 is an unknown quantity at this point 
Solved Threads: 0
fedderico10 fedderico10 is offline Offline
Newbie Poster

Convert xmlChar to int

 
0
  #1
Nov 22nd, 2008
Hey people, i am having the next easy problem but i dont find any answer on how to do that:
Im using libxml2 to read a XML file, in one part of my code i need to compare two attributes from different nodes which are numbers (1,2,3,4,5,6...). The problem is when i say "if (x == y) {}" it always pass it because cant compare xmlChars (at least thats what i think).
Im looking for some easy method to convert those values to int so i can do a comparission and solve the problem.
Thank you in advance!

the code:
  1. for (resource; resource != NULL; resource = resource->next)
  2. {
  3. xmlChar *resource_id;
  4. resource_id = xmlGetProp (resource, (const xmlChar *)"ID");
  5. for (event; event != NULL; event = event->next)
  6. {
  7. xmlChar *event_id;
  8. // event_id = xmlGetProp (event, (const xmlChar *)"ID");
  9. x = xmlGetProp (event "ID");
  10.  
  11. if (resource_id == event_id)
  12. {
  13. std::cout << "parada 1" << std::endl;
  14. }
  15. }
  16. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Re: Convert xmlChar to int

 
0
  #2
Nov 22nd, 2008
You want to use
  1. xmlStrEqual(resource_id, event_id)

it will return 1 if equal and 0 if different.

so

  1. if(xmlStrEqual(resource_id, event_id))
  2. std::cout << "parada 1" << std::endl;

should do the trick.

http://xmlsoft.org/html/libxml-xmlst...ml#xmlStrEqual

also resource_id == event_id does not work since you are just comparing two addresses, not what the values are at the addresses your looking at.
Last edited by Paul.Esson; Nov 22nd, 2008 at 7:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Convert xmlChar to int

 
0
  #3
Nov 22nd, 2008
As far as I know there is
  1. int xmlStrEqual(const xmlChar * str1,
  2. const xmlChar * str2)
function in xmlstring module (and other xmlChar string handling functions). For example, you can convert xml-string to ordinar C-string then use atol to get integer value of attribute - and so on...

I don't use libxml now then can't present ready-to-use code. Look at libxml2 manual...
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Re: Convert xmlChar to int

 
0
  #4
Nov 22nd, 2008
Originally Posted by ArkM View Post
As far as I know there is
  1. int xmlStrEqual(const xmlChar * str1,
  2. const xmlChar * str2)
function in xmlstring module (and other xmlChar string handling functions). For example, you can convert xml-string to ordinar C-string then use atol to get integer value of attribute - and so on...

I don't use libxml now then can't present ready-to-use code. Look at libxml2 manual...
Yea, There is hence the reason I posted about it 58 minutes ago
Last edited by Paul.Esson; Nov 22nd, 2008 at 8:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: fedderico10 is an unknown quantity at this point 
Solved Threads: 0
fedderico10 fedderico10 is offline Offline
Newbie Poster

Re: Convert xmlChar to int

 
0
  #5
Nov 23rd, 2008
Yes that way i can compare to strings but what if i need to compare a string with an integer? how would i do that?
Thanks anyway!
Last edited by fedderico10; Nov 23rd, 2008 at 5:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Re: Convert xmlChar to int

 
0
  #6
Nov 23rd, 2008
I found this thread on the xml gnome mailing list that may be of help. It does appear that there is nothing similar to atoint for an xmlChar *, but I guess you can do it the way that this guy describes or alternatively format a char* from your number (using sscanf) then create a xmlChar* from that then compare those two strings.

Post I found is http://mail.gnome.org/archives/xml/2.../msg00060.html
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC