Possible Duplicate:
Simple and clean xml manipulation in PHP
I trying to reference specific tags and attributes within my XML file. The file format is as follows (please note this is just a small section of the file, its too big to cut and paste entire file):
<?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>
The only tags I want are the <HeadLine>
, <body.content>
and the Href
attribute of <ContentItem>
. My code at the moment simply parses the entire file, outputting everything. All help appreciated.
Update - SimpleXML Example
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->Conte开发者_StackOverflow中文版ntItem->DataContent->nitf;
}
}
The actual output of this is:
stephen
******NewsEnvelope:
NewsItem:
The simplest way to extract a couple of data from an XML file/string is to use SimpleXML :
- First, load the file with
simplexml_load_file()
-- or the string withsimplexml_load_string()
- And, then, use object and array access :
- Object syntax to access nodes and their children :
$node->subnode->subsubnode
- And array syntax to access attributes :
$node['attribute']
- Object syntax to access nodes and their children :
For more complex extractions, you can also use XPath queries, with SimpleXMLElement::xpath()
.
精彩评论