Parse XML responses

Note: See the sample file XMLApiAdapter.java.

When you send an XML request to Adobe Connect, the server returns an XML response. You need to parse the response and extract values, including status codes. One way to parse the response is to use XPath to traverse XML elements (see the XPath tutorial at w3schools.com for more information).

As an example, this is the response from sco-shortcuts :

<?xml version="1.0" encoding="utf-8" ?>  
<results> 
    <status code="ok" />  
    <shortcuts> 
        <sco tree-id="4930295" sco-id="2006258748" type="my-courses"> 
            <domain-name>http://example.com</domain-name>  
        </sco> 
        <sco tree-id="4930293" sco-id="2006258749" type="my-events"> 
            <domain-name>http://example.com</domain-name>  
        </sco> 
        ... 
    </shortcuts> 
</results>

Extract values

The getShortcuts method calls sco-shortcuts and parses the response using XPath. This is an example of how to extract a list of sco elements and the sco-id of each:

public List getShortcuts() throws XMLApiException { 
    try { 
        Element e = request("sco-shortcuts", null); 
        List scosXml = XPath.selectNodes(e, "//sco"); 
        List scos = new ArrayList(); 
        XPath id = XPath.newInstance("./@sco-id"); 
        for (Iterator i = scosXml.iterator(); i.hasNext();) { 
            Element s = (Element) i.next(); 
            SCO sco = getSco(id.valueOf(s)); 
            if(null != sco) 
                 scos.add(sco); 
        }  
        return scos; 
    } catch (JDOMException jde) { 
            throw new XMLApiException(PARSE_ERROR, jde); 
    } 
} 

Extract a status code

Your application also needs to parse both successful and unsuccessful responses for status codes. For example, when a user calls an action without sufficient permission, the error response has a status element with both code and subcode attributes:

<?xml version="1.0" encoding="utf-8" ?>  
<results> 
    <status code="no-access" subcode="denied" /> 
</results>

These lines in the createXPaths method parse for the values of code and subcode :

codePath = XPath.newInstance("//status/@code"); 
subcodePath = XPath.newInstance("//status/@subcode");

In the sample, the createXPaths method is called when you create an instance of XMLApiAdapter. The getStatus method then uses codePath and subcodePath to return the code and subcode values, separated by a hyphen:

private String getStatus(Element el) throws JDOMException { 
        String code = codePath.valueOf(el); 
        String subcode = subcodePath.valueOf(el); 
        StringBuffer status = new StringBuffer(); 
        if(null != code && code.length() > 0) 
            status.append(code); 
        if(null != subcode && subcode.length() > 0) 
            status.append(" - " + subcode); 
        return status.toString(); 
}

// Ethnio survey code removed