I'm going crazy here... again!!
I have this simplexml file:
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.....
I want to have the "DateCreated" in this particular way: 2010年07月08日 The following code works...:
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 = "";
}
}
output: 2010年07月08日 But... i want it without xpath, because i need to have another peace of code in this "foreach loop" that doesn't work with xpath...
This code doesn't doesn't work....:
foreach ($xml->PubmedArticle as $MedlineCitation)
{
foreach ($MedlineCitation->DateCreated as $date)
{
foreach ($date as $ymd)
{
$datecreated .= $ymd . "-";
}
$datecreated = substr($datecreated, 0, -1);
echo $datecreated;
$datecreated = "";
}
}
And this also doesn't wok...:
foreach ($xml->PubmedArticle as $MedlineCitation)
{
$datecreated = "";
$datecreated = $MedlineCitation->DateCreated->Year;
$datecreated = $datecreated . "-" . $MedlineCitation->DateCreated->Month;
$datecreated = $datecreated . "-" . $MedlineCitation->DateCreated->Day;
echo $datecreated;
}
Best regards! Thijs
2 Answers 2
It looks like you're just getting a little confused over how the XML structure is accessed in SimpleXML. To get your dates, you could loop over each MedlineCitation
within each PubmedArticle
like:
// 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;
}
}
If there is only ever one MedlineCitation
within a PubmedArticle
then the inner loop is unnecessary and you could do:
// 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;
}
-
THANKS!! this is the solution! I missed one level in the structure!Thijs– Thijs2010年12月20日 07:58:13 +00:00Commented Dec 20, 2010 at 7:58
Assume that you have loaded xml file content into $xml then try this snippet
//
$yy = $xml->DateCreated->Year; $mm = $xml->DateCreated->Month; $dd = $xml->DateCreated->Day;
$datecreated = $yy.'-'.$mm.'-'.$dd; // yyyy-mm-dd
echo $datecreated;
// Hope this help!
-
It works when i use the whole path in the foreach loop... so it looks likes the $MedlineCitation doesn't fill...? in the: "foreach ($xml->PubmedArticle as $MedlineCitation)" loop...?Thijs– Thijs2010年12月19日 20:36:33 +00:00Commented Dec 19, 2010 at 20:36