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 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)