Loading...
Logo
Processing Forum
I am trying to access profile data via the Facebook Graph Api like described here:

I have to say. I am very unexperienced with XML. 
So this might be a real noob question.


what I get is an XMLElemn which looks like this:

Copy code
  1. <<?xml version="1.0" encoding="UTF-8"?>
  2. <Users_getInfo_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd" list="true">
  3.   <user>
  4.     <uid>1477411208</uid>
  5.     <first_name>Marc</first_name>
  6.     <last_name>Tiedemann</last_name>
  7.   </user>
  8. </Users_getInfo_response>/>

looks good to me. but when I try to access the children, i get null back!
instead everything seems to be root!

Copy code
  1.         println("childs "+ xml.hasChildren());
  2.         println("root "+ xml.getName());

console output:
Copy code
  1. childs false
  2. root <?xml version="1.0" encoding="UTF-8"?>
  3. <Users_getInfo_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd" list="true">
  4.   <user>
  5.     <uid>1477411208</uid>
  6.     ...


I also read that this method of getting info from the Graph API is kinda outdated. As REST got depriciated.
Does anybody have a running example of how you could do this better?
As said I am very very new to this kind of coding and happy for any hint's in the right direction. 


Replies(9)

How do you initialize the xml variable?
Also, are you using 1.5.1 or 2.0b? The XML handling changes a lot between the two versions. The wiki probably refers to the old method.
I am using 1.5.1 for now. Would you recon switching to the new one?

I really just did a straight copy and paste from the wiki. So initialization goes like this:

Copy code
  1.  String xmlResponse = fbCallMethod( new String[] {
  2.                 "method=facebook.Users.getInfo",
  3.                 "uids=" + fbUserIDs,
  4.                 "fields=uid,first_name,last_name", // see link above for more options
  5.                 "format=XML"
  6.         });

  7.  XMLElement xml = new XMLElement( xmlResponse );
  8.         println(xml);
The page you link to uses: 
usersXml = xml.getChildren( "user" );

So it skips the cruft and jumps straight at the useful data.
yeah. but then i get null pointer. check out my first entry. there are no childs. just a big root!
I see the last line:
</Users_getInfo_response> />
If that's the real data and not a copy / paste error, then the XML is wrong and Processing is confused.
aha.. he does that when parsing the String to the XMLElement. So the String is right:

Copy code
  1. <Users_getInfo_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd" list="true">
  2.   <user>
  3.     <uid>1477411208</uid>
  4.     <first_name>Marc</first_name>
  5.     <last_name>Tiedemann</last_name>
  6.   </user>
  7. </Users_getInfo_response>


but XMLElement adds the outer brakets:

Copy code
  1. <<?xml version="1.0" encoding="UTF-8"?>
  2. ...
  3. </Users_getInfo_response>/>

still it shouldn't do this, if i understand right.





      
Try replacing
XMLElement xml = new XMLElement(xmlResponse);
with
XMLElement xml = XMLElement.parse(xmlResponse);

Thanx PhiLho! that did it!

what is the difference here?
I don't have the 1.5 code at hand right now, but I think the difference is that the XMLElement constructor with a string isn't designed to parse an XML string...
IIRC, it is actually made to create a new single node with the given string as tag name.