drmticpet66231422 2014-12-27 21:23
浏览 77
已采纳

嵌套在数据中的Xml php属性

I am working with the following xml

<?xml version="1.0" encoding="UTF-8"?>
<ns2:worldmate-parsing-result xmlns:ns2="http://www.worldmate.com/schemas/worldmate-api-v1.xsd" request-id="100793160" received="2014-12-27T16:34:42.000Z">
    <status>SUCCESS</status>
    <error-msg>An itinerary confirmation e-mail was parsed successfully</error-msg>
    <headers>
        <header value="multipart/alternative;  boundary=&quot;Apple-Mail=_62E5BF7A-C482-4FE0-8F43-15613E46D5FD&quot;" name="Content-type" />
        <header value="&lt;xxxxx@me.com&gt;" name="Return-Path" />
        <header value="rule=notspam policy=default score=0 spamscore=0  suspectscore=3 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0  reason=mlx scancount=1 engine=7.0.1-1412080000 definitions=main-1412270180" name="X-Proofpoint-Spam-Details" />
    </headers>
    <end-user-emails>
        <user email="yyyyy@me.com" />
    </end-user-emails>
    ....
 </ns2:worldmate-parsing-result>

I am trying to access the following data within the xml:

xxxxx@me.com
yyyyy@me.com

I am trying to do access the second email address using the following code.

$endus='end-user-emails';
$endUserEmails=$xml->$endus->attributes()->{'user'};
echo $endUserEmails;

Not sure why but it is forcing me to use a variable name, and if I use dashes I get errors.

  • 写回答

2条回答 默认 最新

  • duanjue6575 2014-12-27 21:55
    关注

    You could use the XML DOM Parser to query the XML:

    $doc = new DOMDocument;
    $doc->Load('file.xml');
    $xpath = new DOMXPath($doc);
    $entries = $xpath->query('//end-user-emails/user/@email');
    foreach ($entries as $entry) {
        echo $entry->nodeValue; //Or do something else with the email value
    }
    

    Crucial is the query:

    //end-user-emails/user/@email
    

    Which means: "For every tag <user> under a tag <end-user-emails>, return the email attribute."

    For the first email this can be replaced by:

    //headers/header/@value
    

    and then remove the &lt; and &gt;

    Example: (with php -a)

    $ php -a Interactive mode enabled

    php > $doc = new DOMDocument();
    php > $xmldoc = '<?xml version="1.0" encoding="UTF-8"?>
    php ' <ns2:worldmate-parsing-result xmlns:ns2="http://www.worldmate.com/schemas/worldmate-api-v1.xsd" request-id="100793160" received="2014-12-27T16:34:42.000Z">
    php '     <status>SUCCESS</status>
    php '     <error-msg>An itinerary confirmation e-mail was parsed successfully</error-msg>
    php '     <headers>
    php '         <header value="multipart/alternative;  boundary=&quot;Apple-Mail=_62E5BF7A-C482-4FE0-8F43-15613E46D5FD&quot;" name="Content-type" />
    php '         <header value="&lt;xxxxx@me.com&gt;" name="Return-Path" />
    php '         <header value="rule=notspam policy=default score=0 spamscore=0  suspectscore=3 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0  reason=mlx scancount=1 engine=7.0.1-1412080000 definitions=main-1412270180" name="X-Proofpoint-Spam-Details" />
    php '     </headers>
    php '     <end-user-emails>
    php '         <user email="yyyyy@me.com" />
    php '     </end-user-emails>
    php '  </ns2:worldmate-parsing-result>';
    php > $doc->loadXML($xmldoc);
    php > $entries = $xpath->query('//end-user-emails/user/@email');
    php > foreach ($entries as $entry) {
    php { echo $entry->nodeValue."
    ";
    php { }
    yyyyy@me.com
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥65 C# datagridview 栏位进度