在不使用XPath php的情况下直接转到implexmlement [英] Go directly to simplexmlelement without xpath php

查看:49
本文介绍了在不使用XPath php的情况下直接转到implexmlement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍然是php的新手…… 我有一个很大的简单exmlstring,我想从文件中获取所有姓氏,以及它们在哪篇文章中:

类似:

文章编号0作者位置0‘van Tricht’
文章编号0作者位置1‘尼曼’
文章编号0作者位置2‘van Tricht’
文章编号0作者位置3‘Bour’
.....
文章编号1作者位置0‘van Tricht’
文章编号1作者位置1‘尼曼’
文章编号1作者位置2‘van Tricht’
文章编号1作者位置3‘Bour’

我想直接转到Author->LastName,但在没有使用XPath的情况下,我用XPath尝试了一下,但后来我的Foreach循环给出了奇怪的输出...这就是我尝试的内容:

<?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;

       }
}
?>

这是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. m.j.vantricht@amc.uva.nl
                                    [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......

推荐答案

为了获得所需的输出,您的循环应如下所示:

$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++;
}

这篇关于在不使用XPath php的情况下直接转到implexmlement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆