0

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

asked Dec 19, 2010 at 20:02

2 Answers 2

1

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;
}
answered Dec 19, 2010 at 22:03
1
  • THANKS!! this is the solution! I missed one level in the structure! Commented Dec 20, 2010 at 7:58
0

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!

answered Dec 19, 2010 at 20:22
1
  • 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...? Commented Dec 19, 2010 at 20:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.