-2

I have the test.xml file where I want these node values to be encoded: ...

<section id="1">
<title id="2">&gt;</title>
<title id="2">&lt;</title>
<title id="2">&lt;</title>
</section>

...

So i wrote this code but after it saves the XML is the same, none of the node values are decoded to "<" from "$lt;"

php code:

$dom = new DOMDocument;
$dom->load("test.xml");
$titles = $dom->getElementsByTagName('title');

foreach ($titles as $title) {

$title->nodeValue=html_entity_decode($title->nodeValue);
}
 $dom->save("success.xml") 

Could you tell me whats wrong with it?

  • There is nothing wrong with that. What you experience is the expected (an only working) behavior. – hakre Mar 12 '13 at 09:45

1 Answers1

1

First of all:

  1. The whole purpose of using a library is getting encoding and decoding done for you automatically.
  2. If you inject a literal < character, you are defining an XML tag.

Assuming you have a valid reason to care about how < and > are encoded, you must use CDATA:

<?php

$dom = new DOMDocument;
$dom->load("test.xml");
$titles = $dom->getElementsByTagName('title');

foreach ($titles as $title) {
    $newNode = $dom->createCDATASection($title->nodeValue);
    $title->nodeValue = null;
    $title->appendChild($newNode);
}
$dom->save("success.xml");

... which generates:

<?xml version="1.0"?>
<section id="1">
<title id="2"><![CDATA[>]]></title>
<title id="2"><![CDATA[<]]></title>
<title id="2"><![CDATA[<]]></title>
</section>

But this document is 100% equivalent to the one you already have (except for the missing <?xml version="1.0"?> heading).

Edit #1: entities are just a syntactic trick to encode special chars, they do not alter the node contents at all, and PHP decodes everything automatically for you:

$dom = new DOMDocument;
$dom->load("test.xml");
$titles = $dom->getElementsByTagName('title');

foreach ($titles as $title) {
    var_dump($title->nodeValue);
}

... prints the decoded contents as expected:

string(1) ">"
string(1) "<"
string(1) "<"

Perhaps it's easier to see with this other example:

echo 'O\'Brian';

... prints O'Brian and not O\'Brian. We're encoding the string, not modifying it.

I suspect that in the whole picture there's some jQuery code like this:

$.get('ajax/data.php', function(data) {
    $('.result').text(data);
});

... where there should be something like:

$.get('ajax/data.php', function(data) {
    $('.result').html(data);
});

Edit #2:

I can't even image how your authoring system works (I hope you aren't using a WYSIWYG editor to actually type HTML tags) but you've asked about XML and in XML there're only two things you can do you with angle brackets:

  1. Data
  2. Tags

In DOMDocument you can create tags with createElement() and you can insert the previously created tags with e.g. appendChild().

Period.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • well the whole reason is encoding some style tags for html, because this : <b> this would be bold text </b> renders as this would be bold text and not really bold text – KukoricaJános Mar 11 '13 at 13:35
  • @KukoricaJános - XML does not have the faintest idea about HTML formatting. See my edit. – Álvaro González Mar 11 '13 at 15:12
  • well first, thanks for your answer. I know that php and html are translating those encoded chars to their normal form but in my case I need to have the normal form like : or or whatever because the css im using for the xml file isnt working on it if its encoded. So all I wanted to do is replace all encoded values to their real form so the css could work. – KukoricaJános Mar 11 '13 at 15:48
  • @KukoricaJános - But you don't have to. Please read my edit again and feel free to ask for further clarifications if something is not clear. – Álvaro González Mar 11 '13 at 15:51
  • I do get the string from a wysiwyg editor which makes <> marks into < and > , and its not a problem until I add a CSS file to the XML. In the CSS im declaring the and other tags as font-weight:bold; ect. so if the xml file is opened in a browser window the text inside tags are formatted as bold. But it only works if theres inside my XML file and not &lg;b$gt; – KukoricaJános Mar 11 '13 at 16:55
  • @KukoricaJános - I've added a second edit but, honestly, I doubt it'll help you at all. I'd recommend that you first try to get a static XML file what works for you (no PHP, just a text editor). – Álvaro González Mar 11 '13 at 17:09
  • Im using the wysiwyg editor called Nicedit for creating the nodevalues of the xml file. I have another interface for selecting the nodes and creating them but that isnt the point now. If I select a node its nodevalue is displayed inside the Nicedit box where I can edit it and even format it with Nicedit. Thats where the formating tags like or come from. – KukoricaJános Mar 11 '13 at 17:25