使用 PHP 从 XML 中的节点内部获取数据 [英] Getting Data from inside a node in XMl with PHP
问题描述
所以我不确定我需要什么或如何真正描述我需要什么,但希望有人能理解.
整个xml文件的单个元素如下所示:
<前>for ( $counter = 1; $counter load($file_xml);//确保路径正确$note = $objDOM->getElementsByTagName("事件");//遍历 XML 提要$i = 0;foreach( $note as $value ){$event_id = $value->getAttribute('ID');# 场地标识$VenueID = $value->getElementsByTagName("Venue");$venue_id = $VenueID->item(0)->getAttribute("ID");}}arrgggg 它并没有真正完全复制他的代码.天啊.
顶部是for循环的迭代.在 foreach 语句中,我有插入语句;这是我尝试插入图像 URL 的地方
<事件名称>事件名称</事件名称><艺术家><艺术家姓名>艺术家姓名</艺术家姓名></艺术家><场地 ID="IDIDIDID"><场地名称>场地名称</场地名称><图像><Url>IMAGE_URL</Url><宽度>205</宽度><高度>115</高度></图片></场地></事件>
我能够获得所需的其余图像,但图像 URL 除外.我试图将它分解为一个数组并获得第一个值,但这没有用.我在这里看到了另一篇文章并尝试使用 preg_match 但这没有用.有谁知道我该怎么做:
获取单个事件"行内图像的 URL?
这是我所拥有的开始:
$Image = $value->getElementsByTagName("Image");$venue_img_2 = $Image->item(1)->nodeValue;
这将返回 IMAGE_URL 205 115
注意:IMAGE_URL 与 205 和 115 之间的空格"实际上并不是空格"
提前致谢.
假设你的 Event 节点有一个父节点(即
):
$xml = simplexml_load_file($file_xml);foreach($xml->Event as $event) {$idAttr = 'ID';$event_id = (string) $event->attributes()->$idAttr;$venue_id = (string) $event->Venue->attributes()->$idAttr;$venue_img_2 = (string) $event->Venue->Image->Url;}
So im not sure what or how to really describe what i need but hopefully someone will understand.
single element of the total xml file looks like this:
for ( $counter = 1; $counter load($file_xml); //make sure path is correct $note = $objDOM->getElementsByTagName("Event"); // Loop through XML feed $i = 0; foreach( $note as $value ) { $event_id = $value->getAttribute('ID'); # venue id $VenueID = $value->getElementsByTagName("Venue"); $venue_id = $VenueID->item(0)->getAttribute("ID"); } }
arrgggg its not really copying he code exactly. geez.
the top is the iteration of the for loop. inside the foreach statement I have the insert statement; which is where I am trying to insert the image URL
<Event ID="IDIDIDIDID">
<EventName>EVENT NAME</EventName>
<Artist>
<ArtistName>ARTIST NAME</ArtistName>
</Artist>
<Venue ID="IDIDIDID">
<VenueName>VENUE NAME</VenueName>
<Image>
<Url>IMAGE_URL</Url>
<Width>205</Width>
<Height>115</Height>
</Image>
</Venue>
</Event>
I am able to get the rest of the imageofmation I need, except for the image URL. i have tried to break it up as an array and get the first value but that didnt work. I saw another post here and tried to use preg_match but that didnt work. does anyone know how i could:
get the URL of the image inside the individual "Event" row?
this is the beginning of what I have:
$Image = $value->getElementsByTagName("Image");
$venue_img_2 = $Image->item(1)->nodeValue;
this will return IMAGE_URL 205 115
Note: the "space" between the IMAGE_URL and 205 and 115 doesn't seem to actually be a "space"
thanks in advance.
Assuming your Event nodes have one parent (ie <Events><Event>...</Event><Event>...</Event></Events>
):
$xml = simplexml_load_file($file_xml);
foreach($xml->Event as $event) {
$idAttr = 'ID';
$event_id = (string) $event->attributes()->$idAttr;
$venue_id = (string) $event->Venue->attributes()->$idAttr;
$venue_img_2 = (string) $event->Venue->Image->Url;
}
这篇关于使用 PHP 从 XML 中的节点内部获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!