0
$result = "<QRYRESULT>
           <ISSUCCESS>Y</ISSUCCESS>
           <TRN_REF>2498297295729857927</TRN_REF>
           <WARNING>IF ANY WARNING</WARNING>
           </QRYRESULT>";

this is an XML string I am using. now I have to add a new node to this string (not a file). That is after adding the new node the XML string will look like the following. I am getting this XML string from another system in a variable so modifying the string manually is not an option.

$result = "<QRYRESULT>
           <ISSUCCESS>Y</ISSUCCESS>
           <TOKEN>some token</TOKEN>
           <TRN_REF>2498297295729857927</TRN_REF>
           <WARNING>IF ANY WARNING</WARNING>
           </QRYRESULT>";

how can I do that ??

jishan
  • 300
  • 1
  • 4
  • 20
  • First convert it into an XML object then add a node to it. Search PHP SimpleXML on SO – Hanky Panky Jun 10 '15 at 07:03
  • possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – michi Jun 10 '15 at 07:27
  • Hi jishan, this question has been asked and answered a zillion times, see the link I posted above, try it and please come back with specific questions fitting the SO format. – michi Jun 10 '15 at 07:29

2 Answers2

0

Use eg. simplexml

$xml = new SimpleXMLElement($yourstring);
$xml->addChild('node_name', 'node_value');
echo $xml->asXML();
venca
  • 1,196
  • 5
  • 18
-2

A variant without using XML and consistency check:

$result=str_replace("</ISSUCCESS>","</ISSUCCESS><TOKEN>some token</TOKEN>",$result);
umka
  • 1,655
  • 1
  • 12
  • 18