Home > Mobile >  use setAttribute and createElement | PHP XML
use setAttribute and createElement | PHP XML

Time:11-17

This code:

$product = $dom->createElement('o');
     
     $product->setAttribute('id', $productSku);
     
     $product->setAttribute('url', $productWebsite.$productUrl);
     
     $product->setAttribute('price', substr_replace($productPrice,'.',-2,0).$productCurrency); 
     
     $product->setAttribute('avail', $productAvail);
     
     $product->setAttribute('weight', $productWeight);
     
     $product->setAttribute('stock', $productStock);
     
     $product->setAttribute('basket', $productBasket);

Generate for me:

<offers><o id="12264" url="http" price="209.22" avail="1" weight="1" stock="183" basket="1">

Now I add:

//tag
     $title = $dom->createElement('name', clean($productTitle)); 

     $product->appendChild($title);

and under this xml line I have

<name>some name</name>

everything is okay! But I need add next tags under this all lines to get result:

<imgs>
      <main url="https://firstimage1"/>
</imgs>

so now I try:

 $product = $dom->createElement('imgs');
$product->setAttribute('main', $productImagePath.$productImage);

What is issue?

  • This code replace first tag o with imgs and tag main add to first tag with <o some like this by end of line:

    <offers><imgs id="12264" url="http" price="209.22" avail="1" weight="1" stock="183" basket="1" main="https:urlphoto">
    

This is wrong. I need to get full effect:

<offers><o id="12264" url="http" price="209.22" avail="1" weight="1" stock="183" basket="1">
<name>some name</name>
<imgs>
          <main url="https://firstimage1"/>
    </imgs>
</o>
</offers>

full code:

      $product = $dom->createElement('o');
         
         $product->setAttribute('id', $productSku);
         
         $product->setAttribute('url', $productWebsite.$productUrl);
         
         $product->setAttribute('price', substr_replace($productPrice,'.',-2,0).$productCurrency); 
         
         $product->setAttribute('avail', $productAvail);
         
         $product->setAttribute('weight', $productWeight);
         
         $product->setAttribute('stock', $productStock);
         
         $product->setAttribute('basket', $productBasket);
        
    
         //tag
         $title = $dom->createElement('name', clean($productTitle)); 
    
         $product->appendChild($title);
         
         // next element (issue here) this replace first tags
         
         $product = $dom->createElement('imgs');
         
         $product->setAttribute('main', $productImagePath.$productImage);



$root->appendChild($product);
     
     

   }

   $dom->appendChild($root); 

   $dom->save($filePath); 

 } 

@update: I try change:

// next element
     
         // next element
 
 $product2 = $dom->createElement('imgs');
 
 $product3 = $dom->createElement('main');
 
 $product3->setAttribute('url', $productImagePath.$productImage);

and by end of file:

 $root->appendChild($product);
 $root->appendChild($product2);
 $root->appendChild($product3);

But now I have:

<offers>
<o id="12264" url="http" price="209.22" avail="1" weight="1" stock="183" basket="1">
<name>4711 Eau De Cologne 800ml</name>
</o>
<imgs/>

@update 2: @Thank you for answear. This working correct! Can I ask you about last thing.

I need add again:

<attrs>
      <a name="Producent">
        <![CDATA[Avery]]>
      </a>
      <a name="EAN">
        <![CDATA[9084692100225]]>
      </a>
      <a name="Kod producenta">
        <![CDATA[AVSG710022]]>
      </a>
    </attrs>

For this I duplicate function:

  // create append "attrs" to "o"
    $offer->appendChild(
      $attrs = $document->createElement('attrs')
    );
    // create append "a" to "attrs"
    $attrs->appendChild(
      $attr = $document->createElement('a')
    );
    // add properties to the "attrs" element
    $attr->setAttribute('producent', $productBrand);
    $attr->setAttribute('ean', $productEan);
    $attr->setAttribute('kod_producent', $productSku);

But currently I've:

   <attrs>
      <a producent="4711" ean="4011700740031" kod_producent="12264"/>
    </attrs>

CodePudding user response:

You named the imgs element node variable $product and overwrite the o element - its parent node. Make sure to use semantic and unique names for your variables.

Use methods on the DOM document to create the nodes and append them to their parent node. appendChild() returns the appended node, so you can use it to chain calls and only need to store the node into a variable for extended modification.

Do not use the second argument of createElement(). It does not fully escape special characters. Set the textContent property or append a text node.

You can append the element node directly after creating it - here is no need to wait. You can modify the appended node at any time - this are the same methods you use to manipulate existing nodes.

Here is an example that creates your target XML structure (with reduced attributes).


$document = new DOMDocument('1.0', 'UTF-8');

// create append the document element "offers"
$document->appendChild(
  $offers = $document->createElement('offers')
);

// create append the "o" element
$offers->appendChild(
  $offer = $document->createElement('o')
);
// add properties to the "o" element
$offer->setAttribute('id', 'SKU123');
$offer->setAttribute('url', 'https://example.com#product');

// create append the "name" to "o"
$offer
  ->appendChild($document->createElement('name'))
  // chain call and set the text content
  ->textContent = 'Example product name';

// create append "imgs" to "o"
$offer->appendChild(
  $images = $document->createElement('imgs')
);
// create append "main" to "imgs"
$images->appendChild(
  $image = $document->createElement('main')
);
// add properties to the "main" element
$image->setAttribute('url', 'https://example.com#product-image');

$document->formatOutput = true;
echo $document->saveXML();

Output:

<?xml version="1.0" encoding="UTF-8"?>
<offers>
  <o id="SKU123" url="https://example.com#product">
    <name>Example product name</name>
    <imgs>
      <main url="https://example.com#product-image"/>
    </imgs>
  </o>
</offers>
  • Related