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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)