2012-10-09 5 views
0

Usurv 설문 조사를 내 웹 사이트에 통합하려고합니다. 이렇게하려면 HTTP POST를 사용하여 URL http://app.usurv.com/API/Gateway.svc/getcampaignforframe에 XML 요청을 제출해야합니다. 그런 다음 응답에는 설문 조사를 가리키는 고유 URL이 있어야합니다. 는 XML을하지 않는Java를 사용하여 XML 게시물 요청 제출

"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>"." 

그것에 대해 정말 혼란 스러워요 : 코드가 제대로 컴파일하지만 나는 다음과 같은 예외가 웹 페이지를로드 할 때 -

는 불행하게도 나는 그것이 동작하지 않습니다 심지어 태그의 링크가있어서 어디에서 오류가 올지 모르겠습니다. 누구든지이 문제의 원인이 될 수있는 아이디어가 있습니까? 어떻게 해결할 수 있습니까?

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); 
     } 
    } 

} 

가}

답변

0

이 줄은 정말 이상한 보이는, 대신 응답 본문을 구문 분석 의미하지 않는다 : 여기

는 자바 코드?

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

문자열 매개 변수를 구문 분석 방법은 URL로이 문자열을 사용하므로 XML 파서는 GET 요청을 사용하여 서버에 다시 연결입니다. 서버가 아마도 HTML 형식의 오류 메시지로 응답하고 있기 때문에 link 요소에 대한 예외가 발생합니다.

다음과 같이 뭔가 잘 작동한다 : 그것에 대해

Document document = docBuilder.parse(new InputSource(new StringReader(response))); 
+0

감사합니다. 불행히도 이제 널 포인터 예외가 발생합니다. 응답 변수는 null이며 서버 응답 (URL이어야 함)을 선택하지 않는 이유는 확실하지 않습니다. – Victoria