469,286 Members | 2,500 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

How to replace a perticular tag's content of an xml file with < instead of &lt; ?

Expand|Select|Wrap|Line Numbers
  1. <contact_us_client_email>
  2.     <message>
  3.         &lt;![CDATA[&lt;link href=\"http:/www.blurbpoint.com/style1.css\" rel=\"stylesheet\" type=\"text/css\" /&gt;
  4. &lt;div style=\"width: 900px; margin-left: auto; margin-right: auto;\"&gt;
  5.     &lt;div style=\"width: 900px; height: 100px; float: left; margin: 0pt 0pt 0pt 0px;\"&gt;
  6.         &lt;img src=\"http://www.blurbpoint.com/images/header.gif\" /&gt;&lt;/div&gt;
  7.     &lt;div style=\"width: 898px; float: left; margin: 0pt 0pt 0pt 0px; border-left: 1px solid rgb(197, 197, 197); border-right: 1px solid rgb(197, 197, 197);\"&gt;
  8.         &lt;div style=\"width: 900px; float: left; margin: 15px 0pt 0pt 0px; padding: 0pt 0pt 0pt 0px;\"&gt;
  9.             &lt;div style=\"width: 840px; font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 12px; color: rgb(35, 35, 35); margin: 0pt 0pt 0pt 14px; line-height: 16px;\"&gt;
  10.                 We have Sucessfully Received Your Inquiry. We will contact you.&lt;/div&gt;
  11.         &lt;/div&gt;
  12.         &lt;div style=\"width: 868px; float: left; margin: 15px 0pt 0pt 15px;\"&gt;
  13.             &lt;div style=\"width: 868px; float: left; font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 25px; color: rgb(6, 91, 157);\"&gt;
  14.                 Customer Support&lt;/div&gt;
  15.             &lt;div style=\"border: 1px dashed rgb(140, 140, 140); width: 868px; float: left; margin: 5px 15px 0pt 0px;\"&gt;
  16.                 &lt;div style=\"width: 93px; float: left; margin: 15px 0pt 0pt 7px;\"&gt;
  17.                     &lt;img src=\"http://www.blurbpoint.com/images/customer_support_icon.gif\" /&gt;&lt;/div&gt;
  18.                 &lt;div style=\"width: 753px; float: left; margin: 15px 15px 10px 0px; font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 12px; color: rgb(40, 40, 40); text-decoration: none; line-height: 16px;\"&gt;
  19.                     Thanks a lot for visiting Blurbpoint.Our Customer Relationship Manager will be get back with top notch solutions soon. For any further queries contact support@blurbpoint.com&lt;/div&gt;
  20.             &lt;/div&gt;
  21.         &lt;/div&gt;
  22.         &lt;div style=\"width: 900px; border-top: 1px solid rgb(214, 214, 214); float: left; margin: 10px 0pt 10px 0px;\"&gt;
  23.             ]]&gt;
  24.     </message>
  25.  
  26.     <header>
  27.        <![CDATA[Content-Type: text/html;]]>From: support@blurbpoint.com
  28.     </header>
  29.  
  30. </contact_us_client_email>
  31.  
I am having problem that, after replacement, i did not get < in place of &lt; or > in place of &gt; because after the replacement output doesn't come in ckeditor where as source shows the code correctly but in that code <[CDATA[ comes like <--[CDATA[ So what should be done? My replacement file is like...

Expand|Select|Wrap|Line Numbers
  1. $doc = new DOMDocument;
  2. $doc->load('xml_mail_format2.xml');
  3. $playlist= $doc->documentElement;
  4.  
  5. $track = $playlist->getElementsByTagName("message")->item(0);
  6.  
  7. $newTrack = $doc->createElement('message',"<![CDATA[".$_POST['editor123']."]]>");
  8.  
  9. $oldtrack= $track->parentNode->replaceChild($newTrack,$track);
  10.  
  11. echo $doc->saveXML();
  12.  
  13. $doc->save("xml_mail_format2.xml");
  14.  
In this file $_POST['editor123'] contains the formatted design from ckeditor.
Feb 7 '11 #1

✓ answered by Dormilich

there might be issues with the variable naming, however, I got a compareable test case running:
Expand|Select|Wrap|Line Numbers
  1. $xml = <<<XML
  2. <root>
  3.     <child1/>
  4. </root>
  5. XML;
  6. $doc = new DOMDocument;
  7. $doc->loadXML($xml);
  8.  
  9. $newChild = $doc->createElement('child1');
  10. $newCDATA = $doc->createCDATASection("<track>song 1</track>");
  11. $newChild->appendChild($newCDATA);
  12.  
  13. $oldChild = $doc->getElementsByTagName("child1")->item(0);
  14. $oldChild->parentNode->replaceChild($newChild, $oldChild);
  15.  
  16. echo $doc->saveXML();
output:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <root>
  3.     <child1><![CDATA[<track>song 1</track>]]></child1>
  4. </root>

17 3919
Dormilich
8,651 Expert Mod 8TB
your intention is correct as you need to put the content in a CDATA section. however, a CDATA section is created differently (and because of that, your "CDATA section" is inserted as comment).
a CDATA section in XML is an own node type, therefore you have to create it explicitly: DOMDocument->createCDATASection($textContent);
Feb 7 '11 #2
Thank you very much for the reply. I just test it & then tell you that if it benefits me or not...will post reply soon for this.
Feb 7 '11 #3
Expand|Select|Wrap|Line Numbers
  1. $doc = new DOMDocument;
  2.  
  3. $doc->load('xml_mail_format2.xml');
  4.  
  5. $playlist= $doc->documentElement;
  6.  
  7. $track = $playlist->getElementsByTagName("message")->item(0);
  8.  
  9. $data = $doc->createCDATASection($data);
  10.  
  11. $newTrack = $doc->createElement('message',$data);
  12.  
  13. $oldtrack= $track->parentNode->replaceChild($newTrack,$track);
  14.  
  15. $doc->saveXML();
  16.  
  17. $doc->save("xml_mail_format2.xml");
  18.  
It is giving me 2 errors that :
1)Warning</b>: DOMDocument::createElement() expects parameter 2 to be string, object given

2)Argument 1 passed to DOMNode::replaceChild() must be an instance of DOMNode, null given
Feb 7 '11 #4
Dormilich
8,651 Expert Mod 8TB
the second error is a follow up of the first error.

you attach a CDATA node like any other node (itís not text, after all), by DOMNode->appendChild()
Feb 7 '11 #5
Should I send you the attachment of my xml & php file?
If you permit i send to you...I am waiting for your kind response. Thanks...
Feb 7 '11 #6
Dormilich
8,651 Expert Mod 8TB
why would you need to send me the files?
Feb 7 '11 #7
because i am not getting the thing done.I am still having errors so i asked to you. So if you permit then i will send you the 2 files.
Feb 7 '11 #8
Dormilich
8,651 Expert Mod 8TB
just post the php code (and the error messages) here.
Feb 7 '11 #9
Error Message :- DOMDocument::createElement() expects parameter 2 to be string, object given.
Expand|Select|Wrap|Line Numbers
  1. $data = $_POST['editor123'];
  2.  
  3. $doc = new DOMDocument;
  4.  
  5. $doc->load('xml_mail_format2.xml');
  6.  
  7. $playlist= $doc->documentElement;
  8.  
  9. $track = $playlist->getElementsByTagName("message")->item(0);
  10.  
  11. $data = $doc->createCDATASection($data);
  12.  
  13. $newTrack = $doc->createElement('message',$data);
  14.  
  15. $oldtrack= $track->parentNode->replaceChild($newTrack,$track);
  16.  
  17. $doc->saveXML();
  18.  
  19. $doc->save("xml_mail_format2.xml");
  20.  
Can you tell me what changes should be made in the above code.Thanks in advance.
Feb 7 '11 #10
Dormilich
8,651 Expert Mod 8TB
line #13 omit the second parameter, you do not have applicable text. line #14 append the CDATA node to the newly created <message> node.
Feb 7 '11 #11
Expand|Select|Wrap|Line Numbers
  1. $newTrack = $doc->createElement('message');
  2.  
  3. $newTrack = $newTrack->appendChild($data);
  4.  
  5. $oldtrack= $track->parentNode->replaceChild($newTrack,$track);
  6.  
Will it work?
Feb 7 '11 #12
Dormilich
8,651 Expert Mod 8TB
Will it work?
try it out!
Feb 7 '11 #13
It is not working. It gives error that:
Warning</b>: DOMNode::replaceChild() [<a href='domnode.replacechild'>domnode.replacechild</a>]: Couldn't fetch DOMCdataSection

Can you do something? It is a bit urgent, if you don't mind.
Feb 7 '11 #14
Dormilich
8,651 Expert Mod 8TB
there might be issues with the variable naming, however, I got a compareable test case running:
Expand|Select|Wrap|Line Numbers
  1. $xml = <<<XML
  2. <root>
  3.     <child1/>
  4. </root>
  5. XML;
  6. $doc = new DOMDocument;
  7. $doc->loadXML($xml);
  8.  
  9. $newChild = $doc->createElement('child1');
  10. $newCDATA = $doc->createCDATASection("<track>song 1</track>");
  11. $newChild->appendChild($newCDATA);
  12.  
  13. $oldChild = $doc->getElementsByTagName("child1")->item(0);
  14. $oldChild->parentNode->replaceChild($newChild, $oldChild);
  15.  
  16. echo $doc->saveXML();
output:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <root>
  3.     <child1><![CDATA[<track>song 1</track>]]></child1>
  4. </root>
Feb 7 '11 #15
Thanks very much, I am trying what you have suggested. If error comes i will post reply.Thank you very much for giving me your precious time.
Feb 7 '11 #16
Thank you, Thank you very much. You have solved my question.All praise to you. Thank you once again.
Feb 7 '11 #17
Dormilich
8,651 Expert Mod 8TB
and that only because Iím reasonably good with JavaScript Ö *gg*
Feb 7 '11 #18

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by higabe | last post: by
reply views Thread by Wolfgang Schwanke | last post: by
14 posts views Thread by Schraalhans Keukenmeester | last post: by
6 posts views Thread by tentstitcher | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.