gpt4 book ai didi

PHP 检查 XML 中是否存在元素

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

我尝试读取 XML 文件,在表中显示数据并将所需数据写入 MySQL 数据库,以便稍后可以将数据提取为 CSV。

XML 中有一组具有相同名称的数据。

每种产品都有不同数量的 < props>(最多 20 个),我想检查元素是否 < props> 是否退出。现在我从 Prop 中获取所有“数据”。

我很难解释,所以我将显示带有特定数据的嵌套元素。编辑:现在是产品的完整数据。

        <?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<id>1</id>
<artnr>3009000555</artnr>
<title>product</title>
<description>Some long text</description>
<brand>
<id>458</id>
<title>Brand</title>
</brand>
<date>11-05-2015</date>
<modifydate>07-05-2016</modifydate>
<variants>
<variant>
<id>35021</id>
<type></type>
<subartnr>3009000555</subartnr>
<ean>85252000555</ean>
<stock>Y</stock>
</variant>
</variants>
<price>
<currency>EUR</currency>
<b2b>9.39</b2b>
<b2c>16.95</b2c>
<vatnl>21</vatnl>
<vatde>19</vatde>
<discount>N</discount>
</price>
<categories>
<category>
<cat>
<id>1</id>
<title>Maincat</title>
</cat>
<cat>
<id>182</id>
<title>Subcat</title>
</cat>
</category>
</categories>
<measures>
<length>4.00</length>
<weight>243</weight>
<packing>10x9x10</packing>
</measures>
<properties>
<prop>
<propid>25</propid>
<property>Farbe</property>
<valueid>208</valueid>
<value>Weiß</value>
</prop>
<prop>
<propid>41</propid>
<property>Geeignet für</property>
<valueid>229</valueid>
<value>Unisex</value>
</prop>
<prop>
<propid>43</propid>
<property>Verpackung</property>
<valueid>236</valueid>
<value>Einzelhandelverpackung (Karton)</value>
</prop>
<prop>
<propid>54</propid>
<property>Gesamturteil</property>
<valueid>289</valueid>
<value>5</value>
</prop>
<prop>
<propid>70</propid>
<property>Garantie</property>
<valueid>326</valueid>
<value>Ja</value>
</prop>
<prop>
<propid>71</propid>
<property>Art der Garantie</property>
<valueid>328</valueid>
<value>Good Warranty Service</value>
</prop>
</properties>
<material>
<id>101</id>
<title>Keramik</title>
</material>
<popularity>3</popularity>
<pics>
<pic>3009000022.jpg</pic>
<pic>3009000022_2.jpg</pic>
</pics>
</product>
<product>

这是我现在所拥有的,但正如我之前所说,我也将不存在的 Prop 作为空数据。 例如::7,编辑。现在是我的整个代码。该产品只有 7 个< Prop >,其他有 9 个或 15 个或其他。所以我只需要显示现有的 Prop 。但我得到了从 prop[0] 到 prop[19] 的所有内容,即使它们不存在。

        <html>
<table id="mytable">
<thead>
<tr>
<th>Artnr</th>
<th>Name</th>
<th>Beschreibung</th>
<th>Hersteller</th>
<th>EAN</th>
<th>Vorrat</th>
<th>Preis EK</th>
<th>Preis VK</th>
<th>Sonderangebot</th>
<th>Kategorie</th>
<th>Eigenschaften</th>
<th>Bild URL</th>
</tr>
</thead>
<tbody>

<?php

$url = ('http://somdom.com/test1.xml');
$xml = simplexml_load_file( urlencode($url), null, true);
$kommaleerz = ', ';
$doppelpunkt = ':';
$bildaddy = 'http://somedom.com/img/product_pic/500/';


foreach ( $xml->product as $prods ) :?>
<tr>
<td><?php echo $prods->artnr; ?></td>
<td><?php echo $prods->title; ?></td>
<td><?php echo $prods->description; ?></td>
<td><?php echo $prods->brand->title; ?></td>
<td><?php echo $prods->variants->variant->ean; ?></td>
<td><?php echo $prods->variants->variant->stock; ?></td>
<td><?php echo $prods->price->b2b; ?></td>
<td><?php echo $prods->price->b2c; ?></td>
<td><?php echo $prods->price->discount; ?></td>
<td><?php echo $prods->categories->category->cat[1]->title; ?></td>
<td><?php if (isset($prods->properties->prop)) {echo $prods->properties->prop[0]->property . $doppelpunkt . $prods->properties->prop[0]->value .$doppelpunkt .'1' .$kommaleerz
.$prods->properties->prop[1]->property . $doppelpunkt . $prods->properties->prop[1]->value .$doppelpunkt .'2' .$kommaleerz
.$prods->properties->prop[2]->property . $doppelpunkt . $prods->properties->prop[2]->value .$doppelpunkt .'3' .$kommaleerz
.$prods->properties->prop[3]->property . $doppelpunkt . $prods->properties->prop[3]->value .$doppelpunkt .'4' .$kommaleerz
.$prods->properties->prop[4]->property . $doppelpunkt . $prods->properties->prop[4]->value .$doppelpunkt .'5' .$kommaleerz
.$prods->properties->prop[5]->property . $doppelpunkt . $prods->properties->prop[5]->value .$doppelpunkt .'6' .$kommaleerz
.$prods->properties->prop[6]->property . $doppelpunkt . $prods->properties->prop[6]->value .$doppelpunkt .'7' .$kommaleerz
.$prods->properties->prop[7]->property . $doppelpunkt . $prods->properties->prop[7]->value .$doppelpunkt .'8' .$kommaleerz
.$prods->properties->prop[8]->property . $doppelpunkt . $prods->properties->prop[8]->value .$doppelpunkt .'9' .$kommaleerz
.$prods->properties->prop[9]->property . $doppelpunkt . $prods->properties->prop[9]->value .$doppelpunkt .'10' .$kommaleerz
.$prods->properties->prop[10]->property . $doppelpunkt . $prods->properties->prop[10]->value .$doppelpunkt .'11' .$kommaleerz
.$prods->properties->prop[11]->property . $doppelpunkt . $prods->properties->prop[11]->value .$doppelpunkt .'12' .$kommaleerz
.$prods->properties->prop[12]->property . $doppelpunkt . $prods->properties->prop[12]->value .$doppelpunkt .'13' .$kommaleerz
.$prods->properties->prop[13]->property . $doppelpunkt . $prods->properties->prop[13]->value .$doppelpunkt .'14' .$kommaleerz
.$prods->properties->prop[14]->property . $doppelpunkt . $prods->properties->prop[14]->value .$doppelpunkt .'15' .$kommaleerz
.$prods->properties->prop[15]->property . $doppelpunkt . $prods->properties->prop[15]->value .$doppelpunkt .'16' .$kommaleerz
.$prods->properties->prop[16]->property . $doppelpunkt . $prods->properties->prop[16]->value .$doppelpunkt .'17' .$kommaleerz
.$prods->properties->prop[17]->property . $doppelpunkt . $prods->properties->prop[17]->value .$doppelpunkt .'18' .$kommaleerz
.$prods->properties->prop[18]->property . $doppelpunkt . $prods->properties->prop[18]->value .$doppelpunkt .'19' .$kommaleerz
.$prods->properties->prop[19]->property . $doppelpunkt . $prods->properties->prop[19]->value .$doppelpunkt .'20' .$kommaleerz
.'Material' . $doppelpunkt . $prods->material->title .$doppelpunkt .'21' ;} ?> </td>
<td><?php echo $bildaddy . $prods->pics->pic[0] . $kommaleerz
.$bildaddy . $prods->pics->pic[1] . $kommaleerz
.$bildaddy . $prods->pics->pic[2] . $kommaleerz
.$bildaddy . $prods->pics->pic[3] . $kommaleerz
.$bildaddy . $prods->pics->pic[4] . $kommaleerz
.$bildaddy . $prods->pics->pic[5] . $kommaleerz
.$bildaddy . $prods->pics->pic[6] . $kommaleerz
.$bildaddy . $prods->pics->pic[7] . $kommaleerz
.$bildaddy . $prods->pics->pic[8] . $kommaleerz
.$bildaddy . $prods->pics->pic[9] . $kommaleerz
.$bildaddy . $prods->pics->pic[10] . $kommaleerz
.$bildaddy . $prods->pics->pic[11] . $kommaleerz
.$bildaddy . $prods->pics->pic[12] . $kommaleerz
.$bildaddy . $prods->pics->pic[13] . $kommaleerz
.$bildaddy . $prods->pics->pic[14] . $kommaleerz
.$bildaddy . $prods->pics->pic[15] . $kommaleerz
.$bildaddy . $prods->pics->pic[16] . $kommaleerz
.$bildaddy . $prods->pics->pic[17] . $kommaleerz
.$bildaddy . $prods->pics->pic[18] . $kommaleerz

; ?></td>
</tr>
<?php endforeach; ?>

</tbody>
</table>
</body>
</html>

最佳答案

SimpleXML 为属性实现不同的接口(interface)。如果您像字符串一样使用它,它将返回第一个匹配元素的内容。如果像列表一样使用它,则可以迭代所有匹配的元素。

$products = new SimpleXmlElement($xml);

foreach ($products->product as $product) {
foreach ($product->properties->prop as $property) {
var_dump((string)$property->property);
}
}

输出:

string(5) "Farbe"
string(13) "Geeignet für"
string(10) "Verpackung"
string(12) "Gesamturteil"
string(8) "Garantie"
string(16) "Art der Garantie"

我建议为您想要读取的每个 propid 定义一个默认值数组。 propid 应该比标题更稳定,并且不特定于语言。在内部循环中,您验证当前的 id 是否存在于该数组中,并分配 valuevalueid

foreach ($products->product as $product) {
// default values for each product property by `prodid`
$row = [
'25' => '',
'41' => ''
];
foreach ($product->properties->prop as $property) {
$id = (string)$property->propid;
if (array_key_exists($id, $row)) {
$row[$id] = (string)$property->value;
}
}
var_dump($row);
}

输出:

array(2) {
[25]=>
string(5) "Weiß"
[41]=>
string(6) "Unisex"
}

这样,结果将始终具有相同的元素数量和顺序。

获取特定元素(如果存在)

要获取特定属性,请使用 Xpath。例如,使用 propid 25 获取属性:

foreach ($products->product as $product) {
$colors = $product->xpath('properties/prop[propid = 25]');
if (count($colors) > 0) {
var_dump((string)$colors[0]->property);
}
}

SimpleXmlElement::xpath() 将始终返回 SimpleXmlElement 对象的数组。验证元素计数以检查 Xpath 表达式是否找到节点。

DOM 甚至允许您直接获取标量值。如果没有节点匹配,它将返回空值。 DOMXpath::evaluate() 的结果取决于表达式。位置路径返回可使用 foreach 遍历的 DOMNodeList。产生标量的表达式将返回标量值。

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);

foreach ($xpath->evaluate('/products/product') as $product) {
$hasColor = $xpath->evaluate('count(properties/prop[propid = 25]) > 0', $product);
if ($hasColor) {
var_dump('Color:', $xpath->evaluate('string(properties/prop[propid = 25]/valueid)', $product));
}
}

输出:

string(6) "Color:"
string(3) "208"

关于PHP 检查 XML 中是否存在元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37161174/

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