473,806 Members | 2,332 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP saving embedded HTML in XML

I hope there is a simple solution to this, but I've been unable to
find it.
$dom = new DomDocument();
$dom->load("test.xml ");

$test = $dom->getElementsByT agName("test");
$test->nodeValue = "<b>test</b>";
$dom->save("test.xml ");
I would like the node in the xml file to look like:
<test><b>test </b></test>

Rather than the encoded version.
&lt;b&gt;test&l t;/b&gt;

What can be done to accomplish this task?

Thank you
dwain

Feb 21 '07 #1
4 2233
dc******@gmail. com wrote:
I hope there is a simple solution to this, but I've been unable to
find it.
$dom = new DomDocument();
$dom->load("test.xml ");

$test = $dom->getElementsByT agName("test");
$test->nodeValue = "<b>test</b>";
$dom->save("test.xml ");
I would like the node in the xml file to look like:
<test><b>test </b></test>

Rather than the encoded version.
&lt;b&gt;test&l t;/b&gt;

What can be done to accomplish this task?

Thank you
dwain
From what I can see, it's not really possible - if it was possible, and
you tried to re-read the resulting xml file back, you would end up with
something like:

....
<test>
<b>
test
</b>
</test>
....

where <bis a child node of the <testnode, rather than part of the
value of the <testnode.

Therefore, entities like < and do need to be encoded / decoded
(possible with html_entity_dec ode()) when dealing with xml.

If, however, you *are* looking to add <bas a child node of <test>, I'm
sure there are functions to handle that ( probably something like $child
= $test->append_child(' b') )
Feb 21 '07 #2
On Feb 21, 12:37 pm, Ian Taylor <m...@ocset.rev erse.previous.w ord.net>
wrote:
dcrac...@gmail. com wrote:
I hope there is a simple solution to this, but I've been unable to
find it.
$dom = new DomDocument();
$dom->load("test.xml ");
$test = $dom->getElementsByT agName("test");
$test->nodeValue = "<b>test</b>";
$dom->save("test.xml ");
I would like the node in the xml file to look like:
<test><b>test </b></test>
Rather than the encoded version.
&lt;b&gt;test&l t;/b&gt;
What can be done to accomplish this task?
Thank you
dwain

From what I can see, it's not really possible - if it was possible, and
you tried to re-read the resulting xml file back, you would end up with
something like:

...
<test>
<b>
test
</b>
</test>
...

where <bis a child node of the <testnode, rather than part of the
value of the <testnode.

Therefore, entities like < and do need to be encoded / decoded
(possible with html_entity_dec ode()) when dealing with xml.

If, however, you *are* looking to add <bas a child node of <test>, I'm
sure there are functions to handle that ( probably something like $child
= $test->append_child(' b') )- Hide quoted text -

- Show quoted text -

Thank you for the advice,

I'm looking to save HTML inside the XML, for that example that text
may be viewed bold when shown in a browser.

------------------------------------ Test.xml
<?xml version="1.0"?>
<ROOT>
<TEST>
<NAME>TEST PRODUCT 01</NAME>
<DESC>This is a test product, this is <b>bold</b>.</DESC>
<PRICE>9.99</PRICE>
</TEST>
</ROOT>

------------------------------------ Test.xsl
<?xml version="1.0"?>
<xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/
Transform">
<xsl:output method="html"/>

<xsl:template match="/">
<html>
<xsl:for-each select="ROOT/TEST">
<P>
<hr />
<xsl:value-of select="NAME" /<br />
<xsl:copy-of select="DESC |text()"/ <br />
<xsl:value-of select="PRICE" /<br />
<hr />

</P>
</xsl:for-each>

</html>
</xsl:template>
</xsl:stylesheet>

------------------------------------------------------- Test.php
<?php

$xsl = new DomDocument();
$xsl->load("test.xsl ");

$inputdom = new DomDocument();
$inputdom->load("test.xml ");

$proc = new XsltProcessor() ;
$xsl = $proc->importStyleshe et($xsl);

$newdom = $proc->transformToDoc ($inputdom);
print $newdom->saveXML();
?>

----------------------------- Result
<?xml version="1.0" standalone="yes "?>
<html><P><hr/>TEST PRODUCT 01<br/>

<DESC>This is a test product, this is <b>bold</b>.</DESC>

<br/>9.99<br/><hr/></P></html>

-------------------------------- The problem

When a XML document is saved by PHP it looks like

<?xml version="1.0"?>
<ROOT>
<TEST>
<NAME>TEST PRODUCT 01</NAME>
<DESC>This is a test product, this is &lt;b&gt;bold&l t;/b&gt;.</
DESC>
<PRICE>9.99</PRICE>
</TEST>
</ROOT>

I need to be able to save the XML Doc after altering it in PHP:
EX:
$node->nodeValue = "This is a test product, this is <b>bold</b>.";
$dom->save("test.xml ");

without encoding the "<b>"'s.

Feb 21 '07 #3
I have 2 different fixes, one is to a PHP function to swap the chaters
as needed:

function fixXML($string) {

for($i=1; $i < strlen($string) ;$i++) {
$subs1 = substr ($string, $i, 4);

switch ($subs1) {
case "&gt;":
$string = substr ($string, 0, $i) . ">" . substr ($string, $i +4,
strlen($string) );
break;
case "&lt;":
$string = substr ($string, 0, $i) . "<" . substr ($string, $i +4,
strlen($string) );
break;
}
}// end for
return $string;
}

<... code ..>

print fixXML($dom->saveXML());
and another way it can be done in the XSL. I thought this method was
allot messer.... and you would need to call for each charater you need
to swap out.

Snips from test.xsl
--------

<xsl:variable name="myString" select="HEADLIN E" />
<xsl:variable name="myNewStri ng">
<xsl:call-template name="Substring Replace">
<xsl:with-param name="stringIn" select="$myStri ng" />
<xsl:with-param name="substring In" select="'&lt;'"/>
<xsl:with-param name="substring Out" select="'%3C'"/>

</xsl:call-template>
</xsl:variable>
<xsl:value-of select="concat( ' input: ',$myString,' output: ',
$myNewString)" />
<xsl:template name="Substring Replace">
<xsl:param name="stringIn" />
<xsl:param name="substring In" />
<xsl:param name="substring Out" />
<xsl:choose>
<xsl:when test="contains( $stringIn,$subs tringIn)">
<xsl:value-of select="concat( substring-before($stringI n,
$substringIn),$ substringOut)" />
<xsl:call-template name="Substring Replace">
<xsl:with-param name="stringIn" select="substri ng-after($stringIn ,
$substringIn)" />
<xsl:with-param name="substring In" select="$substr ingIn" />
<xsl:with-param name="substring Out" select="$substr ingOut" />
</xsl:call-template>
</xsl:when>
<xsl:otherwis e>
<xsl:value-of select="$string In" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

I hope this helps someone.

good luck

Feb 21 '07 #4
On Feb 21, 5:37 pm, Ian Taylor <m...@ocset.rev erse.previous.w ord.net>
wrote:
dcrac...@gmail. com wrote:
I hope there is a simple solution to this, but I've been unable to
find it.
$dom = new DomDocument();
$dom->load("test.xml ");
$test = $dom->getElementsByT agName("test");
$test->nodeValue = "<b>test</b>";
$dom->save("test.xml ");
I would like the node in the xml file to look like:
<test><b>test </b></test>
Rather than the encoded version.
&lt;b&gt;test&l t;/b&gt;
What can be done to accomplish this task?
Thank you
dwain

From what I can see, it's not really possible - if it was possible, and
you tried to re-read the resulting xml file back, you would end up with
something like:

...
<test>
<b>
test
</b>
</test>
...

where <bis a child node of the <testnode, rather than part of the
value of the <testnode.

Therefore, entities like < and do need to be encoded / decoded
(possible with html_entity_dec ode()) when dealing with xml.

If, however, you *are* looking to add <bas a child node of <test>, I'm
sure there are functions to handle that ( probably something like $child
= $test->append_child(' b') )
Hi

You can do it like Ian said:

$elem = $dom->createElement( 'b');
$elem->nodeValue = 'test';
$test->appendChild($e lem);

Or using a document fragment

$fragment = $dom->createDocument Fragment();
$fragment->appendXml('<b> test</b>');
$test->appendChild($f ragment);

Feb 22 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3558
by: Aquarius2431 | last post by:
Hi!, I don't think I have posted to this group before. Have been using PHP on my webserver for a few months now and finding that I like it quite a bit. Here is a question that just occurred to me. I recently created a BBS (Bulletin Board Service) on my website where I allow people to post messages via a form. It just occurred to me that conceivably they could embed php code in their message trying to 'hack' my site. So I
5
2359
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could. Or how can I replace the html &entities; in a string "blablabla&amp;blablabal&amp;balbalbal" with the chars they mean using re.sub? I found out they are stored in an dict . I though about this functionality:
1
2381
by: Robert Misiak | last post by:
Is it possible to save an embedded resource as a real file on the local disk? Robert
1
1016
by: NCrum | last post by:
I can save a date but not a time like 11:01 any ideas, the grid is bound to a wizard generated dataset Thanks for any help
2
3765
by: Peder Y | last post by:
My code is something like this: --------------- Image img = Image.FromFile("somefile.bmp"); FileStream fStream = new FileStream("someBinaryFile.dat"); BinaryWriter bw = new BinaryWriter(fStream); img.Save(bw.BaseStream, ImageFormat.Bmp);
4
3833
by: Kenny ODell | last post by:
I am fairly new to the C# world, and I am seeking a little guidance about saving user information. Consider the following: I have several windows which contain settings for an embedded target. The user will either load the settings from a file (which is why I am writing this question), or type them in manually, or upload them from the target. What I need to know is how to create a XML file. In past projects, we created an INI type...
2
3204
by: Jay Walker | last post by:
I created a custom DataGridColumn based on Marcie Robillard's MSDN Article: Creating Custom Columns for the ASP.NET Datagrid http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/creatingcustomcolumns.asp The problem I am having is that the data in the custom datagridcolumn is not saved to viewstate and after postback, the column does not contain data.
6
8128
by: Jeff | last post by:
Hey (and thank you for reading my post) In visual web developer 2005 express edition I've created a simple website project.. At this website I want users who register to be able to upload a picture of themselves to their profile... I admit that I'm a newbie... but this is how I understand this:
2
8171
by: Owen.Leibman | last post by:
Here is a complete web page: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"> <html> <head> <title>Javascript Embed and onunload</title> <script type="text/javascript"> function useplayer() { var a = document.getElementById("obj1"); var obj = document.createElement("object"); obj.data = "dayenu.mid";
0
10618
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10371
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3850
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.