I have an XML file in which one child has two categories, but with the same name. I want to add one title to each one. How can we do it in PHP?
This is my XML
<root>
<result>
<node>
<title> Some Title Name</title>
<categories>
<category> categor_one </category>
<category> categor_two </category>
</categories>
</node>
<node>
<title> Some Title Name</title>
<categories>
<category> categor_one </category>
<category> categor_tree </category>
</categories>
</node>
</result>
</root>
But I want to obtain this
<root>
<result>
<node>
<title> Some Title Name</title>
<category>categor_one///categor_two </category>
<category1>categor_one///categor_tree</category1>
</node>
</result>
</root>
I managed to impement a function that only gets correctly the category, but if the title is the same it doesn't work as it just creates a new one.
function solve_something($xml, $destination)
{
$xml = simplexml_load_file($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$items = json_decode($json, TRUE);
$products = array();
$product_data = array();
foreach($items['result']['node'] as $item){
$product_data['title'] = $item['title'];
foreach ($item['categories'] as $category) {
if (is_array($category)) {
$product_data['category'] = implode('///', $category);
} else {
$product_data['category'] = $category;
}
}
$products[] = $product_data;
unset($product_data);
}
$path = createXML($products, $destination);
return $path;
}
function createXML($data, $destination)
{
$xmlDoc = new DOMDocument('1.0', 'UTF-8');
$root = $xmlDoc->appendChild($xmlDoc->createElement("root"));
foreach ($data as $key => $product) {
$productA = $root->appendChild($xmlDoc->createElement('product'));
foreach ($product as $key1 => $val) {
if (!empty($val)) {
if ($key1 == 'price' || $key1 == 'tax' || $key1 == 'stockAmount') {
$productA->appendChild($xmlDoc->createElement($key1, $val));
} else {
$ProductKey = $productA->appendChild($xmlDoc->createElement($key1));
$ProductKey->appendChild($xmlDoc->createCDATASection($val));
}
}
}
}
$xmlDoc->formatOutput = true;
fn_rm($destination);
$xmlDoc->save($destination);
return $destination;
}
The output of my code is something like this
<root>
<product>
<title> Some Title Name</title>
<category>categor_one///categor_two </category>
</product>
<product>
<title> Some Title Name</title>
<category>categor_one///categor_tree</category>
</product>
</root>
If you want to keep the data together with the same title, one approach could be to collect that data upfront by using the title as an array key (if it is a valid array key)
When you create the xml, you have the title in the outer foreach loop as the key, and in the inner foreach you can create elements using implode.
Note that in your code you started using product so I took that as a node name.
Example code, which you could use in your code:
$products = array();
$product_data = array();
$xml = simplexml_load_file($xml, "SimpleXMLElement", LIBXML_NOCDATA);
foreach ($xml->result->node as $node) {
$product_data['title'] = (string)$node->title;
foreach($node->categories as $category) {
if (is_array($category)) {
$product_data['category'] = implode('///', $category);
continue;
}
$product_data['category'] = (array)$category->category;
}
$products[(string)$node->title][] = $product_data;
}
$xmlDoc = new DOMDocument('1.0', 'UTF-8');
$root = $xmlDoc->appendChild($xmlDoc->createElement("root"));
foreach ($products as $key => $product) {
$productA = $root->appendChild($xmlDoc->createElement('product'));
$productA->appendChild($xmlDoc->createElement("title", $key));
for ($i = 0; $i < count($product); $i++) {
$productA->appendChild($xmlDoc->createElement("category" . $i, implode("///", $product[$i]["category"])));
}
}
$xmlDoc->formatOutput = true;
echo $xmlDoc->saveXML();
Output
<?xml version="1.0" encoding="UTF-8"?>
<root>
<product>
<title> Some Title Name</title>
<category0> categor_one /// categor_two </category0>
<category1> categor_one /// categor_tree </category1>
</product>
</root>
Php demo
Related
I have 3 xml files with same structure. What I need to do is merge them together via php.
Take a look at my example file below
1.xml
<data>
<product productsku="ABC1" price="550000" pricesale="" hn="1"></product>
<product productsku="ABC2" price="" pricesale="" hn="0"></product>
</data>
2.xml
<data>
<product productsku="ABC2" price="550000" pricesale="" dn="2"></product>
<product productsku="ABC3" price="" pricesale="" dn="0"></product>
</data>
3.xml
<data>
<product productsku="ABC3" price="550000" pricesale="" gn="3"></product>
<product productsku="ABC4" price="" pricesale="" gn="0"></product>
</data>
I would like to get the following result:
<data><product productsku="ABC1" price="550000" pricesale="" hn="1"></product>
<product productsku="ABC2" price="550000" pricesale="" dn="2" hn="0"></product>
<product productsku="ABC3" price="550000" pricesale="" dn="0" gn="3"></product>
<product productsku="ABC4" price="" pricesale="" gn="0"></product>
</data>
The code that I am trying
<?php
$xml1 = file_get_contents('1.xml');
$xml2 = file_get_contents('2.xml');
$targetDom = new DOMDocument();
$targetDom->loadXml($xml1);
$targetXpath = new DOMXpath($targetDom);
$addDom = new DOMDocument();
$addDom->loadXml($xml2);
$addXpath = new DOMXpath($addDom);
// merge attributes of product elements depending on productsku
foreach ($targetXpath->evaluate('//product[#productsku]') as $product) {
$productsku = $product->getAttribute('productsku');
foreach ($addXpath->evaluate('//product[#productsku='.$productsku.']/#*') as $attribute) {
if (!$product->hasAttribute($attribute->name)) {
$product->setAttribute($attribute->name, $attribute->value);
}
}
}
// copy products elements that are not in target dom
foreach ($addXpath->evaluate('//product[#productsku]') as $product) {
$productsku = $product->getAttribute('productsku');
if ($targetXpath->evaluate('count(//product[#productsku='.$productsku.'])') == 0) {
$targetDom->documentElement->appendChild(
$targetDom->importNode($product)
);
}
}
echo $targetDom->saveXml();
I tried the above, it works fine if the SKU is numeric. But my SKU or ID is not a number. And I have 3 xml files
I tried to find the solution on stackoverflow.
But all is not as I expected.
I'm really not good at this. Please help me through a php snippet.
I found the xml_adopt function here: PHP - SimpleXML - AddChild with another SimpleXMLElement
Then it's a matter of loading each file, keeping track of products and overwriting them if the product has only been seen with no price, then adopting them into a single SimpleXMLElement.
<?php
/*
Question Author: Phan Vũ
Question Answerer: Jacob Mulquin
Question: Merge SimpleXML elements by SKU attribute
URL: https://stackoverflow.com/questions/74625741/merge-simplexml-elements-by-sku-attribute
Tags: php, xml
*/
// https://stackoverflow.com/a/11727581/1427345
function xml_adopt($root, $new) {
$node = $root->addChild($new->getName(), (string) $new);
foreach($new->attributes() as $attr => $value) {
$node->addAttribute($attr, $value);
}
foreach($new->children() as $ch) {
xml_adopt($node, $ch);
}
}
$xml = new SimpleXMLElement('<data></data>');
$files = ['1.xml', '2.xml', '3.xml'];
$to_adopt = [];
foreach ($files as $file) {
$load = simplexml_load_file($file);
foreach ($load->product as $product) {
$sku = (string) $product->attributes()['productsku'];
if (!isset($to_adopt[$sku])) {
$to_adopt[$sku] = $product;
} else {
$price = (string) $to_adopt[$sku]->attributes()['price'];
if (empty($price)) {
$to_adopt[$sku]['price'] = $price;
}
$existing_attributes = ((array) $to_adopt[$sku]->attributes())['#attributes'];
foreach ($product->attributes() as $name => $attribute) {
if (!in_array($name, $existing_attributes)) {
$to_adopt[$sku][$name] = $attribute;
}
}
}
}
}
foreach ($to_adopt as $adopt) {
xml_adopt($xml, $adopt);
}
file_put_contents('output.xml', $xml->asXML());
Yields:
<?xml version="1.0"?>
<data>
<product productsku="ABC1" price="550000" pricesale="" hn="1"/>
<product productsku="ABC2" price="550000" pricesale="" hn="0" dn="2"/>
<product productsku="ABC3" price="550000" pricesale="" dn="0" gn="3"/>
<product productsku="ABC4" price="" pricesale="" gn="0"/>
</data>
i have xml the other array i got it but first one cant how can i solve this
Xml structure like this and get an error code in xml to php First one is not array the second one is array
i Coudnt get the first image children "sitename/11.jpg"
Xml like this
[images] => SimpleXMLElement Object ( [image] => Array ( [0] => sitename/15.jpg [1] => sitename/16.jpg [2] => sitename/17.jpg [3] => sitename/18.jpg ) ) )
[images] => SimpleXMLElement Object ( [image] => sitename/11.jpg ))
<root>
<result>
<node>
<categories>somecategory<categories/>
<images>
<image>sitename/15.jpg</image><image>sitename/16.jpg</image><image>sitename/17.jpg</image><image>sitename/18.jpg</image>
</images>
</node>
<node>
<categories>somecategory<categories/>
<images>
<image>sitename/11.jpg</image>
</images>
</node>
</result>
</root>
function solve_something($xml, $destination)
{
$xml = simplexml_load_file($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$items = json_decode($json, TRUE);
$products = array();
$product_data = array();
$row = 1;
foreach ($items['result']['node'] as $item) {
$product_data['Categories'] = 'categories';
if (isset($item['images']['image'])) {
if (is_array($item['images']['image'])) {
foreach ($item['images']['image'] as $key => $image) {
$key++;
if ($key <= 4) {
$image_name = 'image' . $key;
$product_data[$image_name] = isset($image) ? $image : null;
}
}
} else {
$product_data['image'] = isset($image) ? $image : null;
}
}
$path = createXML($products, $destination);
return $path;
}
The other function code its create the xml file
function createXML($data, $destination)
{
$xmlDoc = new DOMDocument('1.0', 'UTF-8');
$root = $xmlDoc->appendChild($xmlDoc->createElement("root"));
foreach ($data as $key => $product) {
$productA = $root->appendChild($xmlDoc->createElement('product'));
foreach ($product as $key1 => $val) {
if (!empty($val)) {
if ($key1 == 'price' || $key1 == 'tax' || $key1 == 'stockAmount') {
$productA->appendChild($xmlDoc->createElement($key1, $val));
} else {
$ProductKey = $productA->appendChild($xmlDoc->createElement($key1));
$ProductKey->appendChild($xmlDoc->createCDATASection($val));
}
}
}
}
$xmlDoc->formatOutput = true;
fn_rm($destination);
$xmlDoc->save($destination);
return $destination;
}
Code create
<root>
<product>
<categories>somecategory<categories/>
<images>
<image1>sitename/15.jpg
<image2>sitename/16.jpg
<image3>sitename/17.jpg
</images>
</product>
<product>
<categories>somecategory<categories/>
<images>
<image1>sitename/15.jpg
<image2>sitename/16.jpg
<image3>sitename/17.jpg
<image4>sitename/18.jpg
</images>
</product>
</root>
But i want
<root>
<product>
<categories>somecategory<categories/>
<images>
<image1>sitename/15.jpg
<image2>sitename/16.jpg
<image3>sitename/17.jpg
</images>
</product>
<product>
<categories>somecategory<categories/>
<images>
<image1>sitename/11.jpg
</images>
</product>
</root>
There are a few issues with the code
If you want to have 3 images, this part if ($key <= 4) { should be lesser or equal than 2.
You don't really have to return anything (or you want to check for false), as you are writing a file, as the save function returns the number of bytes written or false if an error occurred.
Using $key++; like this in the foreach can also be done using a for loop where you can use $i to append after image
Not sure why you want to use createCDATASection but I have left that part out to get the desired result
As you have multiple sections of node, you may use an $product_data array per iteration to add the values to and after the foreach add $product_data to the $products array to prevent overwriting the values for every loop.
The updated code might look like
function solve_something($xml, $destination)
{
$xml = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$items = json_decode($json, TRUE);
$products = array();
foreach ($items['result']['node'] as $item) {
$product_data = array();
$category = $item["categories"];
$product_data["categories"] = $category;
if (isset($item['images']['image'])) {
if (is_array($item['images']['image'])) {
for ($i = 0; $i < count($item['images']['image']); $i++) {
if ($i < 3) $product_data["image" . ($i + 1)] = $item['images']['image'][$i];
}
} else $product_data["image1"] = $item['images']['image'];
}
$products[] = $product_data;
}
createXML($products, $destination);
}
function createXML($data, $destination)
{
$xmlDoc = new DOMDocument('1.0', 'UTF-8');
$root = $xmlDoc->appendChild($xmlDoc->createElement("root"));
foreach ($data as $key => $product) {
$productA = $root->appendChild($xmlDoc->createElement('product'));
$imagesElm = $xmlDoc->createElement('images');
foreach ($product as $key1 => $val) {
if ($key1 == 'price' || $key1 == 'tax' || $key1 == 'stockAmount' || $key1 === "categories") {
$productA->appendChild($xmlDoc->createElement($key1, $val));
} elseif (substr($key1, 0, 5) === "image") {
$imagesElm->appendChild($xmlDoc->createElement($key1, $val));
}
}
$productA->appendChild($imagesElm);
}
$xmlDoc->formatOutput = true;
$xmlDoc->save($destination);
}
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<root>
<result>
<node>
<categories>somecategory</categories>
<images>
<image>sitename/15.jpg</image>
<image>sitename/16.jpg</image>
<image>sitename/17.jpg</image>
<image>sitename/18.jpg</image>
</images>
</node>
<node>
<categories>somecategory</categories>
<images>
<image>sitename/11.jpg</image>
</images>
</node>
</result>
</root>
XML;
solve_something($xml, "result.xml");
The xml in result.xml looks like
<?xml version="1.0" encoding="UTF-8"?>
<root>
<product>
<categories>somecategory</categories>
<images>
<image1>sitename/15.jpg</image1>
<image2>sitename/16.jpg</image2>
<image3>sitename/17.jpg</image3>
</images>
</product>
<product>
<categories>somecategory</categories>
<images>
<image1>sitename/11.jpg</image1>
</images>
</product>
</root>
Here's a sample XML structure:
<Products>
<Product>
<Id>1</Id>
<Name>Product 1</Name>
<Category>MEN</Category>
<Category>Women</Category>
<Product>
<Product>
<Id>2</Id>
<Name>Product 2</Name>
<Category>MEN2</Category>
<Category>Women2</Category>
<Product>
</Products>
And I want the file like this:
<Products>
<Product>
<Id>1</Id>
<Name>Product 1</Name>
<CategoryName>MEN:Women</CategoryName>
<Product>
<Product>
<Id>2</Id>
<Name>Product 2</Name>
<CategoryName>MEN:Women</CategoryName>
<Product>
</Products>
So basically it will search through the nodes in products. If it finds "Category", it will change the name to "CategoryName" and concatenate all the sub-sequent category node values into a single one separated by semicolon.
So I have wrote this small PHP, but not sure how to get this to work.
<?php
$xmlFile = "test.xml" //assume the contents are in the file
$xml = simplexml_load_file($xmlFile);
foreach($xml as $item)
{
$name = $item->Product;
if($name->count()) //check if its a "product" node
{
foreach($item as $i)
{
$category = $i->Category;
}
}
}
?>
Can someone point me to the right direction? I haven't much worked with XML.
Please Use this
<?php
$xmlFile = "test.xml"; //assume the contents are in the file
$xml = simplexml_load_file($xmlFile);
$table = '<Products>';
foreach($xml as $item)
{
$table .= '<Product>';
$table .= '<Id>'.$item->Id.'</Id>';
$table .= '<Name>'.$item->Name.'</Name>';
$table .= '<Category>';
$i = 0;
foreach($item->Category as $cat)
{
if($i>0){
$table .= ':';
}
$table .= $cat;
$i++;
}
$table .= '</Category>';
$table .= '</Product>';
}
$table .= '</Products>';
echo $table;
?>
Try this:
$xml = '<Products>
<Product>
<Id>1</Id>
<Name>Product 1</Name>
<Category>MEN</Category>
<Category>Women</Category>
</Product>
<Product>
<Id>2</Id>
<Name>Product 2</Name>
<Category>MEN2</Category>
<Category>Women2</Category>
</Product>
</Products>';
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML( $xml, LIBXML_NOBLANKS );
$xpath = new DOMXPath( $dom );
$ProductNode = $xpath->query( "//Product" );
if( $ProductNode->length ) {
foreach ( $ProductNode as $node ) {
$category = $node->getElementsByTagName( 'Category' );
$str = '';
// store a reference to the nodes,
// so that they can be deleted later
$del = array();
foreach ( $category as $p ) {
$str .= $p->nodeValue . ':';
$del[] = $p;
}
$str = trim( $str, ':' );
$child = $dom->createElement( 'CategoryName', $str );
$node->appendChild( $child );
foreach ( $del as $p ) {
$p->parentNode->removeChild( $p );
}
}
}
header('content-type: text/xml');
echo $dom->saveXML();
Hope it helps.
Use DomDocument, if you want to modify XML while traversing it:
$xml_obj = new DOMDocument();
$xml_obj->loadXML($xml_string, LIBXML_NOBLANKS );
$xml_obj->preserveWhiteSpace = false;
$xml_obj->formatOutput = true;
$products = $xml_obj->getElementsByTagName('Product');
foreach ($products as $product) {
$cats = array();
$categories = $product->getElementsByTagName('Category');
$tot = $categories->length;
$to_delete = array();
for($i = 0; $i < $tot;$i++) {
$cat = $categories->item($i);
$cats[] = $cat->textContent;
$to_delete[] = $cat;
}
foreach ($to_delete as $delete_node) {
$product->removeChild($delete_node);
}
$product->appendChild($xml_obj->createElement('CategoryName', implode(":", $cats)));
}
print ($xml_obj->saveXML());
I have many categories in my XML file. How can I read all the products in it?
It only reads the first category.
I am including the xml file for you to look at. Here is the PHP code I'm using:
//get products from xml file
foreach($xml->CREATED->CATEGORY as $product){
$atts = $product->PRODUCT->attributes();
$productitemid = $atts['ITEM'];
$title = $product->MODEL;
$rrp = $product->RRP;
$productsdescription = $product->DESCRIPTION;
$prodname = $product->NAME;
echo $productitemid.' - ' ;
// echo $product->id.' - ';
mysql_query("INSERT INTO products (products_id,products_model,products_price,products_status) VALUES ('$productitemid','$title','$rrp','1')");
mysql_query("INSERT INTO products_description (products_id,products_name,products_description) VALUES ('$productitemid','$prodname','$productsdescription')");
}
And here is the XML structure:
<?xml version="1.0" encoding="iso-8859-1"?>
<STOREITEMS>
<CREATED value="Fri Feb 22 1:01:02 GMT 2013">
<CATEGORY id="441" name=" > test1">
<PRODUCT ITEM="12796">
<NAME>test1</NAME>
<MODEL>bb2018</MODEL>
<PRICE>2.28</PRICE>
<RRP>3.99</RRP>
<THUMB>bb2018s.jpg</THUMB>
<IMAGE>bb2018.jpg</IMAGE>
<DESCRIPTION>
test1
</DESCRIPTION>
<POWER/>
<SIZE/>
<ATTRIBUTES NAME="Size" ATTRIBUTEID="2">
<ATTRIBUTEVALUES VALUE="16" TITLE="Small" PRICEADJUST="0.00"/>
<ATTRIBUTEVALUES VALUE="17" TITLE="Medium" PRICEADJUST="0.00"/>
<ATTRIBUTEVALUES VALUE="18" TITLE="Large" PRICEADJUST="0.00"/>
</ATTRIBUTES>
</PRODUCT>
<CATEGORY id="442" name=" > test2">
<PRODUCT ITEM="12805">
<NAME>test2</NAME>
<MODEL>bb2034</MODEL>
<PRICE>0.58</PRICE>
<RRP>1.50</RRP>
<THUMB>bb2034s.jpg</THUMB>
<IMAGE>bb2034.jpg</IMAGE>
<DESCRIPTION>
test2
</DESCRIPTION>
<POWER/>
<SIZE/>
</PRODUCT>
<CATEGORY id="4423" name=" > test3">
<PRODUCT ITEM="13719">
<NAME>test3?</NAME>
<MODEL>BCPG02</MODEL>
<PRICE>2.83</PRICE>
<RRP>4.95</RRP>
<THUMB>bcg02s.jpg</THUMB>
<IMAGE>bcpg02.jpg</IMAGE>
<DESCRIPTION>
test3
</DESCRIPTION>
</PRODUCT>
</CATEGORY>
</CREATED>
</STOREITEMS>
i have done it like this it works. How do i get the products from the categorys then go to the next cateory and get the next porducts the dabase needs them sequence
//i have done it like this it works
$doc = new DOMDocument();
$var = $doc->load('shop.xml');
$root = $doc->documentElement; //root node
$items = $doc->getElementsByTagName('PRODUCT');
$cat = $doc->getElementsByTagName('CATEGORY');
foreach ($cat as $cats){
foreach ($items as $bar)
if ($categoriesid == $b)
$productsid = $bar->getAttribute('ITEM');
$modelcode = $bar->getElementsByTagName('MODEL')->item(0)->nodeValue;
$rrp = $bar->getElementsByTagName('RRP')->item(0)->nodeValue;
$productsdescription = $bar->getElementsByTagName('DESCRIPTION')->item(0)->nodeValue;
$prodname = $bar->getElementsByTagName('NAME')->item(0)->nodeValue;
$categoriesid = $cats->getAttribute('id');
$categoriesname = $cats->getAttribute('name');
}
You could try using DOMDocument like this:
$doc = new DOMDocument();
$var = $doc->load('yourxml.xml');
$root = $doc->documentElement; //root node
$items = $doc->getElementsByTagName('product');
foreach ($items as $bar)
{
$name = $bar->getElementsByTagName('name')->item(0)->nodeValue;
$model = $bar->getElementsByTagName('model')->item(0)->nodeValue;
$price = ....
//do something with the values
}
Please take look on below sample piece of code
hope this help you.
<?php
$xmlString= '<xml Version="1.0">
<created>
<category>
<product id="a"/>
</category>
<category>
<product id="b"/>
</category>
<category>
<product id="c"/>
</category>
<category>
<product id="d"/>
</category>
</created>
</xml>';
$xml = new SimpleXMLElement($xmlString);
foreach ($xml->created->category as $element)
{
foreach($element as $val)
{
echo " product : ".$val->attributes();
}
}
?>
Output of this code will read all product attribute
product : a product : b product : c product : d
i have problem with simpleXml and adding new items. This is my xml:
<?xml version="1.0" encoding="utf-8"?>
<root>
<items>
<item>abc</item>
<item>def</item>
<item>ghi</item>
</items>
</root>
Im using this php code:
$xml = simplexml_load_file("myxml.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$newItem = $sxe->addChild("items");
$newItem->addChild("item", $newValue);
$sxe->asXML("myxml.xml");
This is the result:
<?xml version="1.0" encoding="utf-8"?>
<root>
<items>
<item>abc</item>
<item>def</item>
<item>ghi</item>
</items>
<items>
<item>jkl</item>
</items>
</root>
This creates me new items node, but i want add item to the same already existing items node.
then, you should not create new items node:
$xml = simplexml_load_file("myxml.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$itemsNode = $sxe->items[0];
$itemsNode->addChild("item", $newValue);
$sxe->asXML("myxml.xml");
Have you tried doing the following way
$newItem->root->items[0]->addChild("item","Test");
Or
$newItem->root->items->addChild("item","Test");
You can use this class to SimpleXML objects that accept children append
<?php
class MySimpleXMLElement extends SimpleXMLElement
{
/**
* Add SimpleXMLElement code into a SimpleXMLElement
*
* #param MySimpleXMLElement $append
*/
public function appendXML($append)
{
if ($append) {
if (strlen(trim((string)$append)) == 0) {
$xml = $this->addChild($append->getName());
} else {
$xml = $this->addChild($append->getName(), (string)$append);
}
foreach ($append->children() as $child) {
$xml->appendXML($child);
}
foreach ($append->attributes() as $n => $v) {
$xml->addAttribute($n, $v);
}
}
}
}