dsns47611 2012-10-10 03:18
浏览 261
已采纳

使用simplexml解析特殊字符xml文件

Apologies if there is an obvious answer (and I know there are about 1000 of these similar questions) - but I have spent two days trying to attack this without success. I cannot seem to crack why I get a null response...

Short background: the following works just fine

$xurl= new SimpleXMLElement('https://gptxsw.appspot.com/view/submissionList?formId=GP_v7&numEntries=1', NULL,    TRUE);
$keyname = $xurl->idList->id[0];
echo $keyname;

this provides a response: a unique key like uuid:d0721391-6953-4d0b-b981-26e38f05d2e5

however I try a similar request (which ultimately would be based on first request) and get a failure. I've simplified code as follows...

$xdurl= new SimpleXMLElement('https://gptxsw.appspot.com/view/downloadSubmission?formId=GP_v7[@version=null%20and%20@uiVersion=null]/GP_v7[@key=uuid:d0721391-6953-4d0b-b981-26e38f05d2e5]', NULL, TRUE);
$keyname2 = $xdurl->data->GP_v7->SDD_ID_N[0];
echo $keyname2;

this provides null. And if I try something like echo $xdurl->asXML(); I get an error response from the site (not from PHP).

Do I need to eject from SimpleXMLElement for the second request? I've read about using XPath and about defining the namespace, but I'm not sure that either would be required: the second file does have two namespaces but one of them isn't used and the other has no prefix for elements. Plus I have tried variations of those - enough to think that my problem/error is either more global in nature (or oversight due to inexperience).

For purposes of this request I have no control over the formatting of either XML file.

  • 写回答

2条回答 默认 最新

  • dongxi5494 2012-10-10 16:38
    关注

    Thanks to @Matze for getting me on right track. Issue is that URL has special characters that SimpleXMLElement cannot parse without help.

    Solution: add urlencode() command like the following

    $fixurl = urlencode('https://gptxsw.appspot.com/view/downloadSubmission?formId=GP_v7[@version=null and     @uiVersion=null]/GP_v7[@key=uuid:d0721391-6953-4d0b-b981-26e38f05d2e5]');
    $xdurl= new SimpleXMLElement($fixurl, NULL, TRUE);
    $keyname2 = $xdurl->data->GP_v7->SDD_ID_N[0];
    echo $keyname2;
    

    this provided the answer (in this case 958)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里