Jump to content

Change value in XML


tobeyt23

Recommended Posts

Need to change the value in an XML string tried the following but no luck, any suggestions?

$_xml_string = '<Order>
<EmbeddedDoc DocumentName="TestDoc" DocumentFormat="PDF">jdsahfdhflhfsdlfhdslhsdflsdfhflkdslkfsdhkfhskldhkl</EmbeddedDoc>
<EmbeddedDoc DocumentName="TestDoc1" DocumentFormat="PDF">jdsahfdhflhfsdlfhdslhsdflsdfhflkdslkfsdhkfhskldhkl</EmbeddedDoc>
</Order>';


foreach($_xml_string->Order->EmbeddedDoc as $document)
{
  $document ='##FILE_CONTENT##';
} 
Link to comment
https://forums.phpfreaks.com/topic/293753-change-value-in-xml/
Share on other sites

$_xml_string is not an object.

$_xml_string = '<Order>
        <EmbeddedDoc DocumentName="TestDoc" DocumentFormat="PDF">jdsahfdhflhfsdlfhdslhsdflsdfhflkdslkfsdhkfhskldhkl</EmbeddedDoc>
        <EmbeddedDoc DocumentName="TestDoc1" DocumentFormat="PDF">jdsahfdhflhfsdlfhdslhsdflsdfhflkdslkfsdhkfhskldhkl</EmbeddedDoc>
    </Order>';
    
$xml = simplexml_load_string($_xml_string);

foreach ($xml->xpath('//EmbeddedDoc') as $doc) {
    echo $doc . '<br>';
}

Link to comment
https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502158
Share on other sites


$_xml_string = '<Order>
<EmbeddedDoc DocumentName="TestDoc" DocumentFormat="PDF">jdsahfdhflhfsdlfhdslhsdflsdfhflkdslkfsdhkfhskldhkl</EmbeddedDoc>
<EmbeddedDoc DocumentName="TestDoc1" DocumentFormat="PDF">jdsahfdhflhfsdlfhdslhsdflsdfhflkdslkfsdhkfhskldhkl</EmbeddedDoc>
</Order>';

$xml = simplexml_load_string($_xml_string);

foreach ($xml->xpath('//EmbeddedDoc') as $doc) {
$doc = '##FILE##';
}

print_r($xml);
Link to comment
https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502688
Share on other sites

$doc = '##FILE##';

 

You're only setting a local variable, not manipulating the xml using simplexml.

 

See an example of using simplexml to actually manipulate the xml: http://runnable.com/UnQMA-VaS1tAAABz/how-to-add-and-edit-elements-to-a-xml-using-simplexml-for-php

Link to comment
https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502690
Share on other sites

Still not working, what am I missing?

$_xml_string = '<Order>
<EmbeddedDoc DocumentName="TestDoc" DocumentFormat="PDF">jdsahfdhflhfsdlfhdslhsdflsdfhflkdslkfsdhkfhskldhkl</EmbeddedDoc>
<EmbeddedDoc DocumentName="TestDoc1" DocumentFormat="PDF">jdsahfdhflhfsdlfhdslhsdflsdfhflkdslkfsdhkfhskldhkl</EmbeddedDoc>
</Order>';

          $xml = new SimpleXMLElement($_xml_string);

          foreach ($xml->xpath('//EmbeddedDoc') as $doc) {
              $doc = '##FILE##';
          }

          $newxml = $xml->asXML();
          echo $newxml;
Link to comment
https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502825
Share on other sites

It also works if you change them individually

$xml = new SimpleXMLElement($_xml_string);

$xml->EmbeddedDoc[0] = '##FILE##'  ;
$xml->EmbeddedDoc[1] = '##FILE##'  ;

$newxml = $xml->asXML();
echo '<pre>' . htmlentities($newxml) . '<pre>';
Link to comment
https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502936
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.