0

Usurv 調査を自分の Web サイトに統合しようとしています。これを行うには、HTTP POST を使用して、XML 要求を URL http://app.usurv.com/API/Gateway.svc/getcampaignforframeに送信する必要があります。次に、応答には、調査を指す一意の URL が含まれている必要があります。

残念ながら、私はそれを動作させることができません - コードは正しくコンパイルされますが、ウェブページをロードすると次の例外が発生します:

"WARNING: URL = http://app.usurv.com/API/Gateway.svc/getcampaignforframe
 [Fatal Error] CampaignFrameRequest%3E:6:3: The element type "link" must be terminated by the matching end-tag "</link>"."

XML には link タグさえないので、私はそれについて本当に混乱しているので、エラーがどこから来ているのかわかりません。これを引き起こしている可能性のあるものと、それを修正する方法を知っている人はいますか?

Javaコードは次のとおりです。

public class UsurvSurveyElement extends RenderController 
{
  private static Logger LOG = Logger.getLogger(UsurvSurveyElement.class.getName());
  String xml = "<CampaignFrameRequest xmlns='http://Qsurv/api' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><PartnerId>236</PartnerId><PartnerWebsiteID>45</PartnerWebsiteID><RespondentID>1</RespondentID><RedirectUrlComplete>http://localhost:8080/eveningstar/home</RedirectUrlComplete><RedirectUrlSkip>http://localhost:8080/eveningstar/home</RedirectUrlSkip></CampaignFrameRequest>";
  String strURL = "http://app.usurv.com/API/Gateway.svc/getcampaignforframe";

  @Override
  public void populateModelBeforeCacheKey(RenderRequest renderRequest, TopModel topModel, ControllerContext controllerContext ) 
  {
    super.populateModelBeforeCacheKey( renderRequest, topModel, controllerContext );  

    PostMethod post = new PostMethod(strURL);

    try 
    { 
        // Specify content type and encoding
        // If content encoding is not explicitly specified
        // ISO-8859-1 is assumed
        post.setRequestHeader(
            "Content-type", "text/xml; charset=ISO-8859-1"); 
        LOG.warning("request headers: " +post.getRequestHeader("Content-type"));

        StringRequestEntity requestEntity = new StringRequestEntity(xml);
        post.setRequestEntity(requestEntity);
        LOG.warning("request entity: " +post.getRequestEntity());

        String response = post.getResponseBodyAsString();
        LOG.warning("XML string = " +  xml); 
        LOG.warning("URL = " +  strURL); 
        topModel.getLocal().setAttribute("thexmlresponse",response);

    }
    catch(Exception e)
    {   
        LOG.warning("Errors while executing postMethod "+ e);
    }  

    try
    {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document document = docBuilder.parse(strURL+xml);
        processNode(document.getDocumentElement());
        LOG.warning("doc output = " + document);
    }
    catch(Exception e)
    {   
        LOG.warning("Errors while parsing XML: "+ e);
    }
} 

private void processNode(Node node) {
    // do something with the current node instead of System.out
    LOG.warning(node.getNodeName());

    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node currentNode = nodeList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            //calls this method for all the children which is Element
            LOG.warning("current node: " + currentNode);
            processNode(currentNode);
        }
    }

}

}

4

1 に答える 1

0

この行は非常に奇妙に見えますが、代わりに応答本文を解析するつもりはありませんか?

Document document = docBuilder.parse(strURL+xml);

文字列パラメーターを持つ parse メソッドはこの文字列を URL として使用するため、XML パーサーは GET 要求を使用してサーバーに再度接続します。linkサーバーが HTML 形式のエラー メッセージで応答している可能性があり、要素に関する例外が発生しています。

次のようなものがうまくいくはずです:

Document document = docBuilder.parse(new InputSource(new StringReader(response)));
于 2012-10-09T12:38:53.150 に答える