1

This is the code related to the problem:

$prep = "<select><option>Option 1</option><option selected>Option 1</option></select>"
$td = $dom->createElement('td',$prep);

Solution:

$f = $this->dom->createDocumentFragment();
$f->appendXML($prep);

BUT still a big problem. Any attributes without value ex: selected, disabled (which you can't write as selected="selected") the createElement doesn't allow you to do so.

How can use attributes without value, and not get 1000 erros like now: Warning: DOMDocumentFragment::appendXML() [domdocumentfragment.appendxml]: Entity: line 1: parser error : Specification mandate value for attribute selected Warning: DOMDocumentFragment::appendXML() [domdocumentfragment.appendxml]: Entity: line 2: parser error : chunk is not well balanced

This happens when passing a selected attribute, or disabled.

Sébastien Renauld
  • 19,203
  • 2
  • 46
  • 66
Pian0_M4n
  • 2,505
  • 31
  • 35
  • 3
    What is your problem actually – Aravind.HU Apr 13 '13 at 12:18
  • So in some elements, like for example the value of the TD I send HTML syntax, and it is not interpreted in the saveHTML() when I echo everything, it shows up as `a@a.com` insted of just going bold or whatever. I got – Pian0_M4n Apr 13 '13 at 14:11
  • Found this: [http://stackoverflow.com/questions/2778110/change-innerhtml-of-a-php-domelement] – Jakub Kotrs Apr 13 '13 at 14:16
  • @user2156913: How does your function look? What are you inputting? What are you getting as output? What are you **expecting** to get as output? Without *all* of these, no one will be able to help you. – Madara's Ghost Apr 13 '13 at 14:16

2 Answers2

1

I cannot resist correcting. In PHP's implementation of the DOMDocument model, strings are also objects (this allows you to easily select them rather than to rely on nodeValue, which makes life easier in most cases).

To insert a table row with text, do this:

$f = new DOMDocument();
$table = $f->createElement("table");
$f->appendChild($table);
$tbody = $f->createElement("tbody");
$table->appendChild($tbody);
$tr = $f->createElement("tr");
$tbody->appendChild($tr);
$td = $f->createElement("td");
$tr->appendChild($td);

// Magic happens here
$td->appendChild(new DOMText("this is my text"));

Empty attributes are conserved when inserted properly, by the way - but you're free to use selected="selected", it is still valid provided that its content is the lowercase version of its attribute name ( What does it mean in HTML 5 when an attribute is a boolean attribute? ). If you'd still prefer to just flag it as boolean true, use:

$elem->setAttribute("selected","");

(I just ran the code + mode and got no errors whatsoever without the need to use shutop)

Community
  • 1
  • 1
Sébastien Renauld
  • 19,203
  • 2
  • 46
  • 66
  • the was not what the problem is about. sure, the magic works if it's text you are sending to the child(), but I am sending HTML string and it gets appended as text if you use your method. `$td->appendChild(new DOMText("this is my text"));` insted of this try this `$td->appendChild(new DOMText("this is my text"));` or any kind of append to the DOM of HTML formated syntax. As for selected it does not work because of the appendXML witch reports missuse. – Pian0_M4n Apr 15 '13 at 10:21
  • Every single item in DOMDocument's structure *is* an object. You can load them using `appendXML`, but you're risking the consequences: an attribute without a value is *not* valid XML, and DOMDocument will throw up if you do. This is why I went down the object-creation path. If you really want to bypass that, use `tidy` before appendXML. – Sébastien Renauld Apr 15 '13 at 10:55
  • OBJECT CREATION PATH using TEXT witch is not even necessary. You can use `$td->nodeValue = "your text here";` BUT inserting HTML syntax does not work – Pian0_M4n Apr 15 '13 at 11:41
  • @user2156913: guess what? One is an alias of the other. Can you guess which one? As for HTML - I told you exactly the solution already: you either `appendXML` and provide **proper** XML, or you create the objects one by one. (Or you `tidy` `cleanRepair()` your input first) – Sébastien Renauld Apr 15 '13 at 11:44
  • guess what @sebastian ? Your solutions DOES NOT WORK ! I just tried it. As text yes, but that's easy, I was talking about html code. Maybe you should read again the problem. – Pian0_M4n Apr 15 '13 at 11:49
  • I have said it three times already. I will say it a fourth time: if you want to inject HTML through an **XML** DOM traversal utility, I suggest you actually feed it **VALID** XML. `appendXML()` "" on a documentFragment goes through without a single error. I will say it a fourth time: you can create the objects one by one, or you can feed it valid XML. For all your other uses, there is text. – Sébastien Renauld Apr 15 '13 at 12:00
0

I found the answer my self.
$this->dom = new DOMDocument; all other code and what you build
$tbody = $this->dom->createElement('tbody');
$td = $this->dom->createElement('td');

To put a piece of html code in the TD you can't just paste it or set the value via ->nodeValue = ''. It will output as text, NOT as HTML.

$prep = "<strong>lalala</strong>"; or any other html string // create fragment and append it
@@$f = $this->dom->createDocumentFragment(); @@$f->appendXML($prep);

$td->appendChild($f);
-- it WORKS! but be careful :: Any sort of empty attribute to a HTML tag like <option selected> or <option disabled> will cause errors and fail.
Don't try to set attribute as selected="selected". That will also fail due to the HTML/XML parser.

SO createDocumentFragment() works very well as long as you dont have empty value attributes like selected,disabled or other

Pian0_M4n
  • 2,505
  • 31
  • 35