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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kaushal Elsner
    New Member
    • Jan 2011
    • 23

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

    Code:
    <contact_us_client_email>
        <message>
            &lt;![CDATA[&lt;link href=\"http:/www.blurbpoint.com/style1.css\" rel=\"stylesheet\" type=\"text/css\" /&gt;
    &lt;div style=\"width: 900px; margin-left: auto; margin-right: auto;\"&gt;
    	&lt;div style=\"width: 900px; height: 100px; float: left; margin: 0pt 0pt 0pt 0px;\"&gt;
    		&lt;img src=\"http://www.blurbpoint.com/images/header.gif\" /&gt;&lt;/div&gt;
    	&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;
    		&lt;div style=\"width: 900px; float: left; margin: 15px 0pt 0pt 0px; padding: 0pt 0pt 0pt 0px;\"&gt;
    			&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;
    				We have Sucessfully Received Your Inquiry. We will contact you.&lt;/div&gt;
    		&lt;/div&gt;
    		&lt;div style=\"width: 868px; float: left; margin: 15px 0pt 0pt 15px;\"&gt;
    			&lt;div style=\"width: 868px; float: left; font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 25px; color: rgb(6, 91, 157);\"&gt;
    				Customer Support&lt;/div&gt;
    			&lt;div style=\"border: 1px dashed rgb(140, 140, 140); width: 868px; float: left; margin: 5px 15px 0pt 0px;\"&gt;
    				&lt;div style=\"width: 93px; float: left; margin: 15px 0pt 0pt 7px;\"&gt;
    					&lt;img src=\"http://www.blurbpoint.com/images/customer_support_icon.gif\" /&gt;&lt;/div&gt;
    				&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;
    					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;
    			&lt;/div&gt;
    		&lt;/div&gt;
    		&lt;div style=\"width: 900px; border-top: 1px solid rgb(214, 214, 214); float: left; margin: 10px 0pt 10px 0px;\"&gt;
    			]]&gt;
        </message>
    		
        <header>
           <![CDATA[Content-Type: text/html;]]>From: support@blurbpoint.com
        </header>
    		
    </contact_us_client_email>
    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...

    Code:
    $doc = new DOMDocument;
    $doc->load('xml_mail_format2.xml');
    $playlist= $doc->documentElement;
    	
    $track = $playlist->getElementsByTagName("message")->item(0);
    	
    $newTrack = $doc->createElement('message',"<![CDATA[".$_POST['editor123']."]]>");
    	
    $oldtrack= $track->parentNode->replaceChild($newTrack,$track);
    	
    echo $doc->saveXML();
    
    $doc->save("xml_mail_format2.xml");
    In this file $_POST['editor123'] contains the formatted design from ckeditor.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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->createCDATASec tion($textConte nt);

    Comment

    • Kaushal Elsner
      New Member
      • Jan 2011
      • 23

      #3
      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.

      Comment

      • Kaushal Elsner
        New Member
        • Jan 2011
        • 23

        #4
        Code:
        $doc = new DOMDocument;
        
        $doc->load('xml_mail_format2.xml');
        
        $playlist= $doc->documentElement;
        	
        $track = $playlist->getElementsByTagName("message")->item(0);
        	
        $data = $doc->createCDATASection($data);
        	
        $newTrack = $doc->createElement('message',$data);
        	
        $oldtrack= $track->parentNode->replaceChild($newTrack,$track);
        	
        $doc->saveXML();
        
        $doc->save("xml_mail_format2.xml");
        It is giving me 2 errors that :
        1)Warning</b>: DOMDocument::cr eateElement() expects parameter 2 to be string, object given

        2)Argument 1 passed to DOMNode::replac eChild() must be an instance of DOMNode, null given

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          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( )

          Comment

          • Kaushal Elsner
            New Member
            • Jan 2011
            • 23

            #6
            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...

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              why would you need to send me the files?

              Comment

              • Kaushal Elsner
                New Member
                • Jan 2011
                • 23

                #8
                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.

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  just post the php code (and the error messages) here.

                  Comment

                  • Kaushal Elsner
                    New Member
                    • Jan 2011
                    • 23

                    #10
                    Error Message :- DOMDocument::cr eateElement() expects parameter 2 to be string, object given.
                    Code:
                    $data = $_POST['editor123'];
                    	
                    $doc = new DOMDocument;
                    
                    $doc->load('xml_mail_format2.xml');
                    
                    $playlist= $doc->documentElement;
                    	
                    $track = $playlist->getElementsByTagName("message")->item(0);
                    	
                    $data = $doc->createCDATASection($data);
                    	
                    $newTrack = $doc->createElement('message',$data);
                    	
                    $oldtrack= $track->parentNode->replaceChild($newTrack,$track);
                    		
                    $doc->saveXML();
                    
                    $doc->save("xml_mail_format2.xml");
                    Can you tell me what changes should be made in the above code.Thanks in advance.

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      line #13 omit the second parameter, you do not have applicable text. line #14 append the CDATA node to the newly created <message> node.

                      Comment

                      • Kaushal Elsner
                        New Member
                        • Jan 2011
                        • 23

                        #12
                        Code:
                        $newTrack = $doc->createElement('message');
                        	
                        $newTrack = $newTrack->appendChild($data);
                        	
                        $oldtrack= $track->parentNode->replaceChild($newTrack,$track);
                        Will it work?

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          Will it work?
                          try it out!

                          Comment

                          • Kaushal Elsner
                            New Member
                            • Jan 2011
                            • 23

                            #14
                            It is not working. It gives error that:
                            Warning</b>: DOMNode::replac eChild() [<a href='domnode.r eplacechild'>do mnode.replacech ild</a>]: Couldn't fetch DOMCdataSection

                            Can you do something? It is a bit urgent, if you don't mind.

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              there might be issues with the variable naming, however, I got a compareable test case running:
                              Code:
                              $xml = <<<XML
                              <root>
                                  <child1/>
                              </root>
                              XML;
                              $doc = new DOMDocument;
                              $doc->loadXML($xml);
                              
                              $newChild = $doc->createElement('child1');
                              $newCDATA = $doc->createCDATASection("<track>song 1</track>");
                              $newChild->appendChild($newCDATA);
                              
                              $oldChild = $doc->getElementsByTagName("child1")->item(0);
                              $oldChild->parentNode->replaceChild($newChild, $oldChild);
                              
                              echo $doc->saveXML();
                              output:
                              Code:
                              <?xml version="1.0"?>
                              <root>
                                  <child1><![CDATA[<track>song 1</track>]]></child1>
                              </root>

                              Comment

                              Working...