不使用 xpath php 直接进入 simplexmlelement

发布于 2024-10-07 13:41:23 字数 11224 浏览 1 评论 0原文

对 php 还是很陌生... 我有一个大的 simplexmlstring,我想从文件中获取所有姓氏以及它们在哪篇文章中:

类似:

文章编号 0 作者位置 0 'van Tricht'
文章编号 0 作者位置 1 'Nieman'
文章编号 0 作者位置 2 'van Tricht'
文章编号 0 作者位置 3 'Bour'
......
文章编号 1 作者位置 0 'van Tricht'
文章编号 1 作者位置 1 'Nieman'
文章编号 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. [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

等等............

Still very new to php...
I have a large simplexmlstring and i want to get all the lastnames from the file and in which article they where:

something like:

article number 0 author position 0 'van Tricht'
article number 0 author position 1 'Nieman'
article number 0 author position 2 'van Tricht'
article number 0 author position 3 'Bour'
.....
article number 1 author position 0 'van Tricht'
article number 1 author position 1 'Nieman'
article number 1 author position 2 'van Tricht'
article number 1 author position 3 'Bour'

I want to go directly to Author->LastName but without the use of xpath, i tried it with xpath, but then my foreach loops give strange outputs... this what i tried:

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

       }
}
?>

This is the xml string:

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............

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒绊 2024-10-14 13:41:23

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

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

In order to get the desired output, your loop should look like:

$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++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文