tobeyt23 Posted January 8, 2015 Share Posted January 8, 2015 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##'; } Quote Link to comment https://forums.phpfreaks.com/topic/293753-change-value-in-xml/ Share on other sites More sharing options...
Barand Posted January 8, 2015 Share Posted January 8, 2015 $_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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502158 Share on other sites More sharing options...
tobeyt23 Posted January 8, 2015 Author Share Posted January 8, 2015 Yes, sorry I did do that $_xml_string = simplexml_load_string($_xml_request_data,'SimpleXMLElement', LIBXML_PARSEHUGE); However still cant replace the value. Quote Link to comment https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502160 Share on other sites More sharing options...
tobeyt23 Posted January 8, 2015 Author Share Posted January 8, 2015 The xml EmbeddedDoc contains base64 encoded document so I want to string that out before i save the string to DB. Quote Link to comment https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502164 Share on other sites More sharing options...
tobeyt23 Posted January 12, 2015 Author Share Posted January 12, 2015 Any suggestions still cant seem to get the value to change? Quote Link to comment https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502678 Share on other sites More sharing options...
CroNiX Posted January 12, 2015 Share Posted January 12, 2015 Show your current code if you've changed it and it's still not working. Quote Link to comment https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502686 Share on other sites More sharing options...
tobeyt23 Posted January 12, 2015 Author Share Posted January 12, 2015 $_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); Quote Link to comment https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502688 Share on other sites More sharing options...
CroNiX Posted January 12, 2015 Share Posted January 12, 2015 $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 Quote Link to comment https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502690 Share on other sites More sharing options...
tobeyt23 Posted January 13, 2015 Author Share Posted January 13, 2015 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; Quote Link to comment https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502825 Share on other sites More sharing options...
tobeyt23 Posted January 14, 2015 Author Share Posted January 14, 2015 Any help would be greatful, I am banging my head here! Quote Link to comment https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502928 Share on other sites More sharing options...
Solution CroNiX Posted January 14, 2015 Solution Share Posted January 14, 2015 (edited) In the loop, instead of: $doc = '##FILE##'; Try $doc[0] = '##FILE##'; And when you echo $newxml, use htmlspecialchars() so you see everything, like htmlspecialchars($newxml) Edited January 14, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502931 Share on other sites More sharing options...
tobeyt23 Posted January 14, 2015 Author Share Posted January 14, 2015 THANKS! Quote Link to comment https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502934 Share on other sites More sharing options...
Barand Posted January 14, 2015 Share Posted January 14, 2015 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/293753-change-value-in-xml/#findComment-1502936 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.