【问题标题】:PHP - XML Parser [duplicate]PHP - XML 解析器 [重复]
【发布时间】:2011-04-06 16:15:22
【问题描述】:

可能重复:
Simple and clean xml manipulation in PHP

我试图在我的 XML 文件中引用特定的标签和属性。文件格式如下(请注意,这只是文件的一小部分,太大了,无法剪切和粘贴整个文件):

    <?xml version="1.0" encoding="utf-8"?>
<NewsML>
  <NewsEnvelope>
    <DateAndTime>20110406T131311</DateAndTime>
  </NewsEnvelope>
  <NewsItem>
    <Identification>
      <NewsIdentifier>
        <ProviderId>pa.press.net</ProviderId>
        <DateId>20110406</DateId>
        <NewsItemId>New Media (NEWS)_Showbiz_13_13_11</NewsItemId>
        <RevisionId PreviousRevision="0" Update="N">1</RevisionId>
        <PublicIdentifier>urn:newsml:pa.press.net:20110406:New Media (NEWS)_Showbiz_13_13_11:1</PublicIdentifier>
      </NewsIdentifier>
    </Identification>
    <NewsManagement>
      <NewsItemType FormalName="News" />
      <FirstCreated>20110406T131311</FirstCreated>
      <ThisRevisionCreated>20110406T131311</ThisRevisionCreated>
      <Status FormalName="Usable" />
    </NewsManagement>
    <NewsComponent Duid="P201104061313118547A">
      <NewsLines>
        <CopyrightLine>Copyright (c) Press Association Ltd. 2011, All Rights Reserved.</CopyrightLine>
      </NewsLines>
      <NewsComponent Duid="N0429571302081265946A">
        <NewsComponent>
          <Role FormalName="Main" />
          <NewsLines>
            <HeadLine>Big Brother will go on air again</HeadLine>
            <SlugLine>SHOWBIZ Brother Five Big Brother back</SlugLine>
          </NewsLines>
          <DescriptiveMetadata>
            <SubjectCode>
              <Subject FormalName="HHH" />
            </SubjectCode>
          </DescriptiveMetadata>
          <ContentItem>
            <MediaType FormalName="ComplexData" />
            <MimeType FormalName="text/vnd.IPTC.NITF" />
            <DataContent>
              <nitf>
                <body>
                  <body.head>
                    <hedline>
                      <hl1>Big Brother will go on air again</hl1>
                    </hedline>
                  </body.head>
                  <body.content>
                    <p>Hit reality show Big Brother is returning to television this year.</p>
                    <p>The show, which was on Channel 4, is moving to a new home on Channel 5 and will kick off with a celebrity version in the summer which will be followed by a series featuring members of the public.</p>
                    <p>Channel 5 director of programmes Jeff Ford said: "We're hugely excited to have secured the return of Big Brother for Channel 5, which will form a key part of this year's schedule. </p>
                    <p>"The series has previously captivated a decade of television viewers and we aim to bring Channel 5's energy, optimism and vibrancy to the series."</p>
                    <p>A presenter is yet to be announced, but former host Davina McCall has previously said she wants Emma Willis, who presented the spin-off show Big Brother's Little Brother, in the role, saying: "She loves the programme and she is a great presenter."</p>
                    <p>Big Brother, which was dropped by Channel 4 last year, launched in the UK in 2000 after it had been a big hit in the Netherlands.</p>
                    <p>It made stars of contestants including Ryanair flight attendant Brian Dowling, who went on to present SMTV Live, becoming one of the first openly gay children's TV presenters, and Jade Goody.</p>
                    <p>The dental nurse from Bermondsey in south east London became an unlikely celebrity and remained in the public eye until her death from cervical cancer in 2009.</p>
                    <p>Davina has previously ruled out returning to host the show, saying: "I think Big Brother has got legs and any channel that picks it up would be a very lucky channel but I don't think I'll go back to it. I think I've said goodbye.".</p>
                    <p>The show's creator, Endemol UK, has signed a two-year deal with Channel 5.</p>
                  </body.content>
                </body>
              </nitf>
            </DataContent>
          </ContentItem>
        </NewsComponent>
        <NewsComponent>
          <Role FormalName="Supporting" />
          <NewsComponent Duid="N0430461302082964770A">
            <NewsComponent>
              <Role FormalName="Main" />
              <ContentItem Href="Showbiz 1-1.jpg">
                <MediaType FormalName="Photo" />
                <MimeType FormalName="image/jpeg" />
                <Characteristics>
                  <SizeInBytes>17859</SizeInBytes>
                </Characteristics>
              </ContentItem>
            </NewsComponent>
            <NewsComponent>
              <Role FormalName="Caption" />
              <ContentItem>
                <MediaType FormalName="Text" />
                <MimeType FormalName="text/plain" />
                <DataContent>Jade Goody was one of the most famous faces of Big Brother</DataContent>
              </ContentItem>
            </NewsComponent>
          </NewsComponent>
        </NewsComponent>
      </NewsComponent>

我想要的唯一标签是&lt;HeadLine&gt;&lt;body.content&gt;&lt;ContentItem&gt;Href 属性。我现在的代码只是解析整个文件,输出所有内容。感谢所有帮助。

更新 - SimpleXML 示例

echo "stephen";
        echo "<br/>";
        if(!$xml = simplexml_load_file('Showbiz.xml')){
            echo 'unable to load XML file';
        }
        else
        {
            echo "***";
            echo $xml->NewsML->NewsEnvelope->DateAndTime;
            echo $xml->NewsML->NewsItem->NewsComponent->NewsComponent->ContentItem->DataContent->nitf;
            echo "***";
            foreach($xml->children() as $child){
                //echo 'Story: '.$story->HeadLine.'<br />';
                echo $child->getName().': '.$child.'<br />';
                echo $xml->ContentItem['Href'] . '<br />';
                echo $xml->ContentItem->DataContent->nitf;
            }
        }

这个的实际输出是:

stephen
******NewsEnvelope:

NewsItem:

【问题讨论】:

  • 鉴于 StackOverflow 上有很多关于如何使用 SimpleXml 和 DOM 的示例,我想知道您在提出问题之前检查了多少示例,或者您尝试使其工作的内容到目前为止你自己的。听起来很像“gimme teh codez”。
  • 这是一个基本的 SimpleXML 示例:us2.php.net/manual/en/simplexml.examples.php
  • 查看位于屏幕右侧的大量问题。这是一本值得阅读的好书:stackoverflow.com/questions/4343402/…
  • Pascal,我在这方面已经有一段时间了,并尝试了许多解决方案。但就是无法让它工作。我已经接近但只需要帮助。我已经在 stackoverflow 上呆了足够长的时间了,我总是在寻求帮助之前尝试让这些东西正常工作。
  • 至于你的更新:$xml-&gt;NewsML 是错误的,因为&lt;NewsML&gt;$xml 中,因为那是根节点。

标签: php xml parsing


【解决方案1】:

从 XML 文件/字符串中提取几个数据的最简单方法是使用 SimpleXML

  • 首先,加载带有simplexml_load_file()的文件——或者带有simplexml_load_string()的字符串
  • 然后,使用对象和数组访问:
    • 访问节点及其子节点的对象语法:$node-&gt;subnode-&gt;subsubnode
    • 访问属性的数组语法:$node['attribute']

对于更复杂的提取,您还可以使用 XPath 查询,SimpleXMLElement::xpath()

【讨论】:

  • 暂时搁置属性位,我尝试使用 simplexml 但无济于事。我将使用我尝试过的示例更新我的原始问题。也许我做错了。
  • 我使用 simpleXML 让它工作了,对于其他阅读本文的人来说,请特别注意上面 Gordon 的评论。
猜你喜欢
  • 2019-05-15
  • 2010-09-16
  • 1970-01-01
  • 2011-09-05
  • 1970-01-01
  • 2011-11-26
  • 2011-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多