JDOM cannot parse Facebook XML

View: New views
1 Messages — Rating Filter:   Alert me  

JDOM cannot parse Facebook XML

by DinDon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I wanted to use JDOM to parse the XML returned by Facebook but with no luck.

Example:

<?xml version="1.0" encoding="UTF-8"?>

<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">
  <user>
    <uid>622204888</uid>
    <sex>male</sex>
    <name>My Full Name</name>
  </user>
</users_getInfo_response>

And my test case:

public class FacebookXmlTestCase extends TestCase {
        private static Logger log = Logger.getLogger(FacebookXmlTestCase.class);

        public void testTemp() throws Exception {

                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
                                .newInstance();

                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

                // Parse the XML file and build the Document object in RAM
                org.w3c.dom.Document doc = (org.w3c.dom.Document) docBuilder.parse(new File(
                                "src/test/java/users_getInfo_response.xml"));

                //Document jdomDoc = new DOMBuilder().build(doc);
                //Element root = jdomDoc.getRootElement();

                XPathFactory factory = XPathFactory
                .newInstance(XPathFactory.DEFAULT_OBJECT_MODEL_URI);
                XPath xpath = factory.newXPath();

                log.info(xpath.evaluate(
                                "/users_getInfo_response/user/uid", doc));
               
                Node node = (Node) xpath.evaluate("/users_getInfo_response/user/name", doc, XPathConstants.NODE);
                log.info("node.getNodeValue(): " + node);
               
        }
}

I am able to convert the w3c document to jdom document but whatever I try to get the Element or e.getAttributeValue("name"), it is always NULL.

I tried w3c parser and javax.xml.xpath, they worked but jdom is easier than w3c so I wanted to use jdom.

Please advice.

Thanks a lot & regards,
LightInTheBox - Buy quality products at wholesale price