检索所有 google 个标签 xml
Retrieving all google tags xml
我正在解析一个 xml 文件,但我有一些关于标签 (":g") 的问题,我无法访问信息,他的内容,问题是当我尝试获取类别时,我有多个类别。
xml:
<item>
<g:id>4011700742288</g:id>
<title><![CDATA[4711 Acqua Colonia Blood Orange & Basil Eau de Cologne 170ml]]></title>
<link><![CDATA[https://url/asdasd.html]]></link>
<g:image_link><![CDATA[https://url/media/catalog/product/4/7/4711-acqua-colonia-blood-_2.jpg]]></g:image_link>
<g:price>34.86 EUR</g:price>
<g:product_type><![CDATA[Mulher]]></g:product_type>
<g:product_type><![CDATA[Homem]]></g:product_type>
<g:product_type><![CDATA[Unisexo]]></g:product_type>
</item>
我尝试获取类别,例如:
$categories = $item->children('g', TRUE)->product_type;
但是只带了第一个类,没有带其余的类。
上面是我如何获取数据的代码示例。
例如:
foreach($rss->channel->item as $item) {
$categories = $item->children('g', TRUE)->product_type;
// bringing in to array <content:encoded> items from SimpleXMLElement Object()
$content = xmlObjToArr($item->children('content', true)->encoded);
echo $categories . PHP_EOL;
return;
}
function xmlObjToArr($obj) {
$namespace = $obj->getDocNamespaces(true);
$namespace[NULL] = NULL;
$children = array();
$attributes = array();
$name = strtolower((string)$obj->getName());
$text = trim((string)$obj);
if( strlen($text) <= 0 ) {
$text = NULL;
}
// get info for all namespaces
if(is_object($obj)) {
foreach( $namespace as $ns=>$nsUrl ) {
// atributes
$objAttributes = $obj->attributes($ns, true);
foreach( $objAttributes as $attributeName => $attributeValue ) {
$attribName = strtolower(trim((string)$attributeName));
$attribVal = trim((string)$attributeValue);
if (!empty($ns)) {
$attribName = $ns . ':' . $attribName;
}
$attributes[$attribName] = $attribVal;
}
// children
$objChildren = $obj->children($ns, true);
foreach( $objChildren as $childName=>$child ) {
$childName = strtolower((string)$childName);
if( !empty($ns) ) {
$childName = $ns.':'.$childName;
}
$children[$childName][] = xmlObjToArr($child);
}
}
}
return array(
'name'=>$name,
'text'=>$text,
'attributes'=>$attributes,
'children'=>$children
);
}
您的代码是正确的。
$categories = $item->children('g', TRUE)->product_type;
这会将 $categories
设置为一个对象,让您可以访问所有 <g:product_type>
元素。
你的问题是当你写的时候:
echo $categories . PHP_EOL;
这显示 单个 XML 元素的文本内容。由于 $categories
是多个元素的集合,所以 SimpleXML 猜测您想要第一个。换句话说,它相当于:
echo (string)$categories[0] . PHP_EOL;
其中(string)
提取文本内容,由echo
隐含,[0]
获取集合中的第一项
循环遍历元素集合的工作方式与您期望列表的工作方式完全相同 - 您使用 foreach
:
foreach ( $categories as $cat ) {
echo $cat . PHP_EOL;
}
我正在解析一个 xml 文件,但我有一些关于标签 (":g") 的问题,我无法访问信息,他的内容,问题是当我尝试获取类别时,我有多个类别。
xml:
<item>
<g:id>4011700742288</g:id>
<title><![CDATA[4711 Acqua Colonia Blood Orange & Basil Eau de Cologne 170ml]]></title>
<link><![CDATA[https://url/asdasd.html]]></link>
<g:image_link><![CDATA[https://url/media/catalog/product/4/7/4711-acqua-colonia-blood-_2.jpg]]></g:image_link>
<g:price>34.86 EUR</g:price>
<g:product_type><![CDATA[Mulher]]></g:product_type>
<g:product_type><![CDATA[Homem]]></g:product_type>
<g:product_type><![CDATA[Unisexo]]></g:product_type>
</item>
我尝试获取类别,例如:
$categories = $item->children('g', TRUE)->product_type;
但是只带了第一个类,没有带其余的类。 上面是我如何获取数据的代码示例。 例如:
foreach($rss->channel->item as $item) {
$categories = $item->children('g', TRUE)->product_type;
// bringing in to array <content:encoded> items from SimpleXMLElement Object()
$content = xmlObjToArr($item->children('content', true)->encoded);
echo $categories . PHP_EOL;
return;
}
function xmlObjToArr($obj) {
$namespace = $obj->getDocNamespaces(true);
$namespace[NULL] = NULL;
$children = array();
$attributes = array();
$name = strtolower((string)$obj->getName());
$text = trim((string)$obj);
if( strlen($text) <= 0 ) {
$text = NULL;
}
// get info for all namespaces
if(is_object($obj)) {
foreach( $namespace as $ns=>$nsUrl ) {
// atributes
$objAttributes = $obj->attributes($ns, true);
foreach( $objAttributes as $attributeName => $attributeValue ) {
$attribName = strtolower(trim((string)$attributeName));
$attribVal = trim((string)$attributeValue);
if (!empty($ns)) {
$attribName = $ns . ':' . $attribName;
}
$attributes[$attribName] = $attribVal;
}
// children
$objChildren = $obj->children($ns, true);
foreach( $objChildren as $childName=>$child ) {
$childName = strtolower((string)$childName);
if( !empty($ns) ) {
$childName = $ns.':'.$childName;
}
$children[$childName][] = xmlObjToArr($child);
}
}
}
return array(
'name'=>$name,
'text'=>$text,
'attributes'=>$attributes,
'children'=>$children
);
}
您的代码是正确的。
$categories = $item->children('g', TRUE)->product_type;
这会将 $categories
设置为一个对象,让您可以访问所有 <g:product_type>
元素。
你的问题是当你写的时候:
echo $categories . PHP_EOL;
这显示 单个 XML 元素的文本内容。由于 $categories
是多个元素的集合,所以 SimpleXML 猜测您想要第一个。换句话说,它相当于:
echo (string)$categories[0] . PHP_EOL;
其中(string)
提取文本内容,由echo
隐含,[0]
获取集合中的第一项
循环遍历元素集合的工作方式与您期望列表的工作方式完全相同 - 您使用 foreach
:
foreach ( $categories as $cat ) {
echo $cat . PHP_EOL;
}