gpt4 book ai didi

php - 当 2 个或多个字段具有相同名称时,使用 PHP 从 XML 提要中获取数据

转载 作者:行者123 更新时间:2023-11-29 03:11:32 25 4
gpt4 key购买 nike

我在使用它时遇到了一些问题,我有一个大量的 XML 提要,我正在使用 PHP 将其放入 MYSQL 数据库中。

一篇 XML 文章看起来像这样......

<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, speaking 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/>&lt;promo&gt;At NewsReach we can provide quality content for all online marketing platforms - call Jacey on 02075172240 to learn more.&lt;/promo&gt;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 &quot;unlock new segments of consumers&quot;, 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>

我想特别说的部分是:

- <Categories>
<Category ID="800089637">Online Marketing</Category>
<Category ID="800089646">ZHEADER</Category>
<Category ID="800092440">ZREVIEW</Category>
</Categories>

如您所见,我有 3 个类别和 3 个 ID 属性。使用此方法获取数据时:

$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']}");
}

我只得到 3 个类别中的第一个,所以我正在寻找一些帮助来掌握接下来的两个类别,我不知道我是否需要在 foreach 循环中执行 while 循环来获取它们或什么,但是我卡住了,所以我希望有人能提供帮助。

谢谢。

-------- 扩展 --------

我刚试过

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

还是不行

最佳答案

$article->Categories->Category是一个数组,其中包含每个 <Category> 的条目XML 中的元素。在这种情况下,有三个 Category元素,因此您需要遍历每个元素。

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

关于php - 当 2 个或多个字段具有相同名称时,使用 PHP 从 XML 提要中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7571953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com