问题描述
<item>
<g:id><![CDATA[147]]></g:id>
<g:title><![CDATA[ Kemerli Pantolon Dar Kesim 300-07 Pembe]]></g:title>
<g:description><![CDATA[ Kemerli Pantolon Dar Kesim 300-07 Pembe Özellikleri: RENK : Pembe KUMAŞ : g Cotton,0 Viscose,%3 Lycra PANTOLON BOY : 92 cm MANKENİN ÜSTÜNDEKİ ÜRÜNÜN bedENİ : 38 MEVSİM : Dört Mevsim AKSESUAR : Aksesuarlar ürüne dahil değildir. MANKENİN ÖLÇÜLERİ : Basen : 90 cm Bel : 60 cm Göğüs : 88 cm Boy : 168 cm Kilo : 50 kg ]]></g:description>
<g:link><![CDATA[https://www.tesetturpazari.com/kemerli-pantolon-dar-kesim-300-07-pembe]]></g:link>
<g:image_link><![CDATA[https://cdn.tesetturpazari.com/kemerli-pantolon-dar-kesim-300-07-pembe-1134-14-B.jpg]]></g:image_link>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:identifier_exists>yes</g:identifier_exists>
<g:price><![CDATA[29.91 TRY]]></g:price>
<g:brand><![CDATA[Dar Kesim Pantolon]]></g:brand>
<g:google_product_category><![CDATA[Kıyafet ve Aksesuarlar > Giyim Eşyaları > Pantolonlar]]></g:google_product_category>
<g:product_type><![CDATA[Giyim > Pantolon > Dar Kesim Pantolon]]></g:product_type>
<g:custom_label_0>Degil</g:custom_label_0>
<g:sale_price><![CDATA[]]></g:sale_price>
<g:sale_price_effective_date><![CDATA[]]></g:sale_price_effective_date>
<g:gtin><![CDATA[300-07]]></g:gtin>
<g:gender>kadın</g:gender>
<g:age_group>adult</g:age_group>
<g:additional_image_link>https://cdn.tesetturpazari.com/kemerli-pantolon-dar-kesim-300-07-pembe-dar-kesim-pantolon-dar-kesim-pantolon-dar-kesim-1134-14-B.jpg</g:additional_image_link>
<g:additional_image_link>https://cdn.tesetturpazari.com/kemerli-pantolon-dar-kesim-300-07-pembe-dar-kesim-pantolon-dar-kesim-pantolon-dar-kesim-1135-14-B.jpg</g:additional_image_link>
<g:additional_image_link>https://cdn.tesetturpazari.com/kemerli-pantolon-dar-kesim-300-07-pembe-dar-kesim-pantolon-dar-kesim-pantolon-dar-kesim-1136-14-B.jpg</g:additional_image_link>
</item>
我正在尝试使用XMLReader类解析此文件并将其保存到数据库。我将所有noteType解析如下。
$xmlReader = new XMLReader();
$xmlReader->open('http://.........');
while ($xmlReader->read()) {
if ($xmlReader->nodeType == XMLReader::ELEMENT && $xmlReader->localname == "item") {
$xmlReader->read();
$item = array();
}
if ($xmlReader->nodeType == XMLReader::ELEMENT && $xmlReader->prefix == "g") {
if ($xmlReader->localName == 'id') {
$xmlReader->read();
$item['g:id'] = $xmlReader->value;
}
if ($xmlReader->localName == 'title') {
$xmlReader->read();
$item['g:title'] = $xmlReader->value;
}
if ($xmlReader->localName == 'description') {
$xmlReader->read();
$item['g:description'] = $xmlReader->value;
}
if ($xmlReader->localName == 'link') {
$xmlReader->read();
$item['g:link'] = $xmlReader->value;
}
if ($xmlReader->localName == 'image_link') {
$xmlReader->read();
$item['g:image_link'] = $xmlReader->value;
}
if ($xmlReader->localName == 'condition') {
$xmlReader->read();
$item['g:condition'] = $xmlReader->value;
}
if ($xmlReader->localName == 'availability') {
$xmlReader->read();
$item['g:availability'] = $xmlReader->value;
}
if ($xmlReader->localName == 'identifier_exists') {
$xmlReader->read();
$item['g:identifier_exists'] = $xmlReader->value;
}
if ($xmlReader->localName == 'price') {
$xmlReader->read();
$item['g:price'] = $xmlReader->value;
}
if ($xmlReader->localName == 'brand') {
$xmlReader->read();
$item['g:brand'] = $xmlReader->value;
}
if ($xmlReader->localName == 'google_product_category') {
$xmlReader->read();
$item['g:google_product_category'] = $xmlReader->value;
}
if ($xmlReader->localName == 'product_type') {
$xmlReader->read();
$item['g:product_type'] = $xmlReader->value;
}
if ($xmlReader->localName == 'custom_label_0') {
$xmlReader->read();
$item['g:custom_label_0'] = $xmlReader->value;
}
if ($xmlReader->localName == 'sale_price') {
$xmlReader->read();
$item['g:sale_price'] = $xmlReader->value;
}
if ($xmlReader->localName == 'sale_price_effective_date') {
$xmlReader->read();
$item['g:sale_price_effective_date'] = $xmlReader->value;
}
if ($xmlReader->localName == 'gtin') {
$xmlReader->read();
$item['g:gtin'] = $xmlReader->value;
$items[] = $item;
}
}
}
$date = new DateTime('Now');
$kayit_tarih = $date->format('Y-m-d H:i:s');
$kontrol_tarih = $date->format('Y-m-d');
foreach ($items as $value) {
$id = $value['g:id'];
$description = $value['g:description'];
$title = $value['g:title'];
$link = $value['g:link'];
$image_link = $value['g:image_link'];
$condition = $value['g:condition'];
$availability = $value['g:availability'];
$identifier_exists = $value['g:identifier_exists'];
$price = $value['g:price'];
$brand = $value['g:brand'];
$google_product_category = $value['g:google_product_category'];
$product_type = $value['g:product_type'];
$sale_price = $value['g:sale_price'];
$sale_price_effective_date = $value['g:sale_price_effective_date'];
$gtin = $value['g:gtin'];
$in_up_Data = $this->model("readerModel")->xmlInsert($id,$title,$description,$condition,$link,$image_link,$availability,$identifier_exists,$price,$brand,$google_product_category,$product_type,$sale_price,$sale_price_effective_date,$gtin,"Boyutlandırılmadı",$kayit_tarih,$kontrol_tarih);
}
但是
<g:additional_image_link>https://cdn.tesetturpazari.com/kemerli-pantolon-dar-kesim-300-07-pembe-dar-kesim-pantolon-dar-kesim-pantolon-dar-kesim-1134-14-B.jpg</g:additional_image_link>
<g:additional_image_link>https://cdn.tesetturpazari.com/kemerli-pantolon-dar-kesim-300-07-pembe-dar-kesim-pantolon-dar-kesim-pantolon-dar-kesim-1135-14-B.jpg</g:additional_image_link>
<g:additional_image_link>https://cdn.tesetturpazari.com/kemerli-pantolon-dar-kesim-300-07-pembe-dar-kesim-pantolon-dar-kesim-pantolon-dar-kesim-1136-14-B.jpg</g:additional_image_link>
我该怎么办?你能帮我吗?
解决方法
通过将每个<item>
节点导入到SimpleXMLElement中,可以简化XMLReader的许多样板代码。这样一来便可以轻松处理每个完整元素。
在此代码中,每个<item>
使用一个foreach()
循环将所有子元素提取到一个单独的数组($item
数组)中。如果已经遇到此项目,则将该项目转换为一系列项目,然后将每个后续项目添加到列表中...
$xmlReader = new XMLReader();
$xmlReader->open('http://.........');
$doc = new DOMDocument('1.0','UTF-8');
while ($xmlReader->read()) {
if ($xmlReader->nodeType == XMLReader::ELEMENT && $xmlReader->localName == "item") {
// Load the XML under the <item> tag into SimpleXML
$data = simplexml_import_dom($doc->importNode($xmlReader->expand(),true));
$item = [];
// Loop over the child elements from the 'g' namespace
foreach ($data->children("g",true) as $tag => $value ) {
$tag = "g:".$tag;
// If an item with that tag name already exists,convert it to an array of items
if ( isset($item[$tag]) ) {
if ( !is_array($item[$tag])) {
$item[$tag] = [$item[$tag]];
}
$item[$tag][] = (string)$value;
}
else {
// For normal items,add in the value (as a string) to the array
$item[$tag] = (string) $value;
}
}
// Store new item
$items[] = $item;
}
}
所以您的数组将包含类似...
Array
(
[g:id] => 147
[g:title] => Kemerli Pantolon Dar Kesim 300-07 Pembe
[g:description] => Kemerli Pantolon Dar Kesim 300-07 Pembe Özellikleri: RENK : Pembe KUMAŞ : g Cotton,0 Viscose,%3 Lycra PANTOLON BOY : 92 cm MANKENİN ÜSTÜNDEKİ ÜRÜNÜN BEDENİ : 38 MEVSİM : Dört Mevsim AKSESUAR : Aksesuarlar ürüne dahil değildir. MANKENİN ÖLÇÜLERİ : Basen : 90 cm Bel : 60 cm Göğüs : 88 cm Boy : 168 cm Kilo : 50 kg
[g:link] => https://www.tesetturpazari.com/kemerli-pantolon-dar-kesim-300-07-pembe
[g:image_link] => https://cdn.tesetturpazari.com/kemerli-pantolon-dar-kesim-300-07-pembe-1134-14-B.jpg
[g:condition] => new
[g:availability] => in stock
[g:identifier_exists] => yes
[g:price] => 29.91 TRY
[g:brand] => Dar Kesim Pantolon
[g:google_product_category] => Kıyafet ve Aksesuarlar > Giyim Eşyaları > Pantolonlar
[g:product_type] => Giyim > Pantolon > Dar Kesim Pantolon
[g:custom_label_0] => Degil
[g:sale_price] =>
[g:sale_price_effective_date] =>
[g:gtin] => 300-07
[g:gender] => kadın
[g:age_group] => adult
[g:additional_image_link] => Array
(
[0] => https://cdn.tesetturpazari.com/kemerli-pantolon-dar-kesim-300-07-pembe-dar-kesim-pantolon-dar-kesim-pantolon-dar-kesim-1134-14-B.jpg
[1] => https://cdn.tesetturpazari.com/kemerli-pantolon-dar-kesim-300-07-pembe-dar-kesim-pantolon-dar-kesim-pantolon-dar-kesim-1135-14-B.jpg
[2] => https://cdn.tesetturpazari.com/kemerli-pantolon-dar-kesim-300-07-pembe-dar-kesim-pantolon-dar-kesim-pantolon-dar-kesim-1136-14-B.jpg
)
)
您将不得不决定如何处理这些项目,但这将为您提取数据。