Using PHP to grab data from an XML feed when 2 or more fields have the same name
I am having some issues getting this to work, I have a massive XML feed which I am putting into a MYSQL Database using PHP.
One XML article looks like this...
<Article Created="10:49:51" ID="800737873">
<Heading>Week in review: Mobile, Google+ and Facebook grab headlines</Heading>
<Date>23/09/2011</Date>
- <Contents>
- <![CDATA[ This week NewsReach has been busy at ad:tech, sp开发者_开发百科eaking to lots of people about how we can help websites with targeted online newsfeeds, landing pages and other copy.<br/><br/>But we still found time to keep our finger on the pulse of the wider digital world and bring those stories straight to our readers.<br/><br/>This week saw a big focus on mobile marketing, with several industry commentators highlighting the importance of the channel for online communications.<br/><br/><promo>At NewsReach we can provide quality content for all online marketing platforms - call Jacey on 02075172240 to learn more.</promo>Digital trainer <a href="http://www.newsreach.co.uk/nr/online-marketing/marketings-future-is-mobile">Rob Thurner noted</a> that mobile allows businesses to "unlock new segments of consumers", while research by Head London highlighted the importance of good mobile websites.<br/><br/><a href="http://www.newsreach.co.uk/nr/online-marketing/mobile-websites-should-aid-integrated-experience">Report authors suggested</a> that mobile websites should not simply copy the desktop version's content and functionality, but should be designed in line with what mobile users want.<br/><br/>Mobile devices, in particular smartphones, are integral to young adults, according to a <a href="http://www.newsreach.co.uk/nr/online-marketing/internet-integral-to-young-consumers">study by Cisco</a>. The telecoms systems provider found under-30s regard it as vitally important to be able to access the internet on the go.<br/><br/>After about two months in limited field trial stage, <a href="http://www.newsreach.co.uk/nr/social-media-marketing/google-opens-doors-and-rolls-out-search">Google+ is now available to anyone</a> who wants to sign up. Google also rolled out a new search function, which will allow users to get personalised web search results within the social network.<br/><br/>And after much speculation, <a href="http://www.directnews.co.uk/news/facebook-overhauls-profiles-with-timeline-$21378257.htm">Facebook announced yesterday</a> that it will overhaul users' profile pages by introducing a virtual scrapbook that allows members to chronicle their life in one place. <br/><br/><em>Written by <a href="http://uk.linkedin.com/in/karenwebber">Karen Webber</a>, Deputy Head of News Feeds</em>
]]>
</Contents>
- <Summary>
- <![CDATA[ The future is mobile, while social networks continue their battle for supremacy.
]]>
</Summary>
- <Picture Orientation="Landscape" PhotoTag="Mobile, Google+ and Facebook grab headlines" Ratio="1.00" PhotoID="7036189">
<PhotoTag>Mobile, Google+ and Facebook grab headlines</PhotoTag>
- <Large Width="500" Height="500">
<URL>http://pictures.directnews.co.uk/liveimages/mobile+google+and+facebook+grab+headlines_3166_800737873_1_0_7036189_500.jpg</URL>
</Large>
- <Medium Width="300" Height="300">
<URL>http://pictures.directnews.co.uk/liveimages/mobile+google+and+facebook+grab+headlines_3166_800737873_1_0_7036189_300.jpg</URL>
</Medium>
- <Small Width="100" Height="100">
<URL>http://pictures.directnews.co.uk/liveimages/mobile+google+and+facebook+grab+headlines_3166_800737873_1_0_7036189_100.jpg</URL>
</Small>
</Picture>
- <Categories>
<Category ID="800089637">Online Marketing</Category>
<Category ID="800089646">ZHEADER</Category>
<Category ID="800092440">ZREVIEW</Category>
</Categories>
</Article>
The part that I want to specifically talk about is this:
- <Categories>
<Category ID="800089637">Online Marketing</Category>
<Category ID="800089646">ZHEADER</Category>
<Category ID="800092440">ZREVIEW</Category>
</Categories>
As you can see I have 3 Categories and 3 ID attributes. When getting the data using this method:
$feed = new SimpleXMLElement('newsreacharchive2011.xml', null, true);
foreach($feed as $article) // loop through
{
$category = mysql_real_escape_string("{$article->Categories->Category}");
$categoryID = mysql_real_escape_string("{$article->Categories->Category['ID']}");
}
I only get the 1st of the 3 categories, so I am looking for some help getting hold of the next two categories, I dont know if I need to do a while loop within the foreach loop to get them or what, but im stuck so I hope someone can help.
Thanks.
------- EXTENSION -------
I have just tried this
$feed = new SimpleXMLElement('newsreacharchive2011.xml', null, true);
foreach($feed as $article) // loop through
{
$i = 0;
while($i < 3)
{
$category[$i] = mysql_real_escape_string("{$article->Categories->Category}");
$categoryID[$i] = mysql_real_escape_string("{$article->Categories->Category['ID']}");
$i++;
}
}
and it still does not work
$article->Categories->Category
is an array with entries for each <Category>
element in the XML. In this case, there are three Category
elements, so you need to iterate over each one.
$feed = new SimpleXMLElement('newsreacharchive2011.xml', null, true);
foreach($feed as $article) // loop through
{
foreach($article->Categories->Category as $category) {
$categoryName = mysql_real_escape_string((string)$category);
$categoryID = mysql_real_escape_string((string)$category['ID']);
// add to database
}
}
精彩评论