我尝试在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>
我想要的唯一标签是<HeadLine>
,<body.content>
和Href
的{{1}}属性。我的代码目前只是解析整个文件,输出所有内容。所有帮助表示赞赏。
<ContentItem>
这个的实际输出是:
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;
}
}
答案 0 :(得分:1)
从XML文件/字符串中提取一些数据的最简单方法是使用SimpleXML:
simplexml_load_file()
加载文件,或使用simplexml_load_string()
$node->subnode->subsubnode
$node['attribute']
对于更复杂的提取,您还可以使用SimpleXMLElement::xpath()
。