2010-12-15 5 views
0

encore très nouveau pour php ... J'ai un grand simplexmlstring et je veux obtenir tous les lastnames du fichier et dans lequel l'article: ils oùAller directement à SimpleXMLElement sans XPath php

quelque chose comme:

article numéro 0 position auteur 0 'van Tricht'
numéro de l'article 0 de position de l'auteur 1 'Nieman'
numéro de l'article 0 de position de l'auteur 2 'van Tricht'
numéro de l'article 0 position d'auteur 3 'Bour'
.....
article numéro 1 position d'auteur 0 'van Tricht'
numéro de l'article 1 de la position de l'auteur 1 'Nieman'
numéro de l'article 1 de la position de l'auteur 2 'van Tricht'
numéro de l'article 1 position d'auteur 3 « Bour »

Je veux aller directement à Author->LastName mais sans l'utilisation de XPath, je l'ai essayé avec XPath, mais mes boucles foreach donner des sorties étranges ... ce que j'ai essayé:

<?php 
$i = 0; 
$j = 0; 
foreach ($xml->xpath('//AuthorList') as $AuthorList) { 
    $i ++; 
    $j = 0; 
    foreach ($xml->xpath('//Author') as $Author) { 
    $j ++; 
    echo $i . "articlenumber " . "author position" . $AuthorList->Author->LastName; 

     } 
} 
?> 

C'est la chaîne xml:

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 
             ) 

            [Journal] => SimpleXMLElement Object 
             (
              [ISSN] => 1090-2147 
              [JournalIssue] => SimpleXMLElement Object 
               (
                [@attributes] => Array 
                 (
                  [CitedMedium] => Internet 
                 ) 

                [Volume] => 73 
                [Issue] => 3 
                [PubDate] => SimpleXMLElement Object 
                 (
                  [Year] => 2010 
                  [Month] => Aug 
                 ) 

               ) 

              [Title] => Brain and cognition 
              [ISOAbbreviation] => Brain Cogn 
             ) 

            [ArticleTitle] => Increased saccadic rate during smooth pursuit eye movements in patients at Ultra High Risk for developing a psychosis. 
            [Pagination] => SimpleXMLElement Object 
             (
              [MedlinePgn] => 215-21 
             ) 

            [Abstract] => SimpleXMLElement Object 
             (
              [AbstractText] => Abnormalities in eye tracking are consistently observed in schizophrenia patients and their relatives and have been proposed as an endophenotype of the disease. The aim of this study was to investigate the performance of patients at Ultra High Risk (UHR) for developing psychosis on a task of smooth pursuit eye movement (SPEM). Forty-six UHR patients and twenty-eight age and education matched controls were assessed with a task of SPEM and psychiatric questionnaires. Our results showed that both the corrective and non-corrective saccadic rates during pursuit were higher in the UHR group. There were however no differences in smooth pursuit gain between the two groups. The saccadic rate was related to positive UHR symptoms. Our findings indicate that abnormalities in SPEM are already present in UHR patients, prior to a first psychotic episode. These abnormalities occur only in the saccadic system. 
              [CopyrightInformation] => 2010 Elsevier Inc. All rights reserved. 
             ) 

            [Affiliation] => Department of Psychiatry, Academic Medical Center, University of Amsterdam, The Netherlands. [email protected] 
            [AuthorList] => SimpleXMLElement Object 
             (
              [@attributes] => Array 
               (
                [CompleteYN] => Y 
               ) 

              [Author] => Array 
               (
                [0] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => van Tricht 
                  [ForeName] => M J 
                  [Initials] => MJ 
                 ) 

                [1] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Nieman 
                  [ForeName] => D H 
                  [Initials] => DH 
                 ) 

                [2] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Bour 
                  [ForeName] => L J 
                  [Initials] => LJ 
                 ) 

                [3] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Boerée 
                  [ForeName] => T 
                  [Initials] => T 
                 ) 

                [4] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Koelman 
                  [ForeName] => J H T M 
                  [Initials] => JH 
                 ) 

                [5] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => de Haan 
                  [ForeName] => L 
                  [Initials] => L 
                 ) 

                [6] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Linszen 
                  [ForeName] => D H 
                  [Initials] => DH 
                 ) 

               ) 

             ) 

            [Language] => eng 

etc ............

+0

Il ne semble pas y avoir de XML/XPath dans cette question. –

Répondre

0

Afin d'obtenir la sortie désirée, votre boucle devrait ressembler à:

$i = 0; 
foreach ($xml->xpath('//AuthorList') as $AuthorList) { 
    $j = 0; 
    foreach ($AuthorList->Author as $Author) { 
     echo join(' ', array('article number', $i, 'author position', $j, $Author->LastName)); 
     $j++; 
    } 
    $i++; 
}