从没有xpath的simplexml对象获取字符串

时间:2010-12-19 20:02:03

标签: php arrays simplexml

我在这里疯了......再次!!

我有这个simplexml文件:

SimpleXMLElement Object
(
    [PubmedArticle] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [MedlineCitation] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [Owner] => NLM
                                    [Status] => In-Process
                                )

                            [PMID] => 20538400
                            [DateCreated] => SimpleXMLElement Object
                                (
                                    [Year] => 2010
                                    [Month] => 07
                                    [Day] => 08
                                )

                            [Article] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [PubModel] => Print-Electronic
                                        )
etc.....

我希望以这种特殊的方式拥有“DateCreated”:2010-07-08 以下代码有效......:

foreach ($xml->xpath('//MedlineCitation') as $MedlineCitation)
{

    foreach ($MedlineCitation->DateCreated as $date)
    {
             foreach ($date as $ymd)
             {
             $datecreated .= $ymd . "-"; 
             }
     $datecreated = substr($datecreated, 0, -1);
     echo $datecreated;
     $datecreated = "";
     }
}

输出:2010-07-08 但是......我想要它没有xpath,因为我需要在这个“foreach循环”中有另一个代码的安静,它不适用于xpath ......

此代码不起作用....:

foreach ($xml->PubmedArticle as $MedlineCitation)
{
       foreach ($MedlineCitation->DateCreated as $date)
       {
          foreach ($date as $ymd)
          {
          $datecreated .= $ymd . "-"; 
          }
        $datecreated = substr($datecreated, 0, -1);
        echo $datecreated;
        $datecreated = "";
        }
} 

这也不会......:

foreach ($xml->PubmedArticle as $MedlineCitation)
    {
    $datecreated = "";

    $datecreated = $MedlineCitation->DateCreated->Year;
    $datecreated = $datecreated . "-" . $MedlineCitation->DateCreated->Month;
    $datecreated = $datecreated . "-" . $MedlineCitation->DateCreated->Day;
    echo $datecreated;
    }

祝你好运! 泰斯

2 个答案:

答案 0 :(得分:1)

看起来你只是对如何在SimpleXML中访问XML结构感到困惑。要获取您的日期,您可以在每个MedlineCitation内圈出每个PubmedArticle,例如:

// Loop over each <PubmedArticle>
foreach ($xml->PubmedArticle as $article)
{
    // Loop over each <MedlineCitation> within this <PubmedArticle>
    foreach ($article->MedlineCitation as $citation) {
        // Get formatted created date
        $date_created = sprintf(
            "%04d-%02d-%02d",
            $citation->DateCreated->Year,
            $citation->DateCreated->Month,
            $citation->DateCreated->Day
        );
        echo $date_created . PHP_EOL;
    }
}

如果MedlineCitation中只有一个PubmedArticle,那么内部循环是不必要的,您可以这样做:

// Loop over each <PubmedArticle>
foreach ($xml->PubmedArticle as $article)
{
    // Use the <MedlineCitation> within this <PubmedArticle>
    $citation = $article->MedlineCitation;
    // Get formatted created date
    $date_created = sprintf(
        "%04d-%02d-%02d",
        $citation->DateCreated->Year,
        $citation->DateCreated->Month,
        $citation->DateCreated->Day
    );
    echo $date_created . PHP_EOL;
}

答案 1 :(得分:0)

假设您已将xml文件内容加载到$ xml中,然后尝试此代码段

//

$ yy = $ xml-&gt; DateCreated-&gt;年; $ mm = $ xml-&gt; DateCreated-&gt;月; $ dd = $ xml-&gt; DateCreated-&gt; Day;

$ datecreated = $ yy .'-'。$ mm。' - '。$ dd; // yyyy-mm-dd

echo $ datecreated;

// 希望这有帮助!