Javascript DOM Parser incompatible with Document?

Asked

Viewed 86 times

1

My idea with the DOM Parser was to load HTML layouts, place them in a single element, and then use them when necessary. But in tests, the object generated by DOMParser not added to Document. See the example I did in Jsfiddle:

HTML:

<div id="resHtml">
</div>
<div id="resDOM">
</div>
<div id="resXML">
</div>

Javascript

var txt = '<fieldset><legend>Em texto HTML</legend><p>Alguma coisa a dizer</p></fieldset>';
document.getElementById("resHtml").innerHTML = txt;
// Simples, mas aí tenho que já ter em texto, e não dentro de um XML.
var domDoc = document.createElement('fieldset');
var domLegend = document.createElement('legend');
domLegend.innerHTML = 'Em DOM';
var domTexto = document.createElement('p');
domTexto.innerHTML = 'Alguma coisa a dizer';
domDoc.appendChild(domLegend);
domDoc.appendChild(domTexto);
document.getElementById("resDOM").appendChild(domDoc);
// Esse é construído peça por peça, não serve, mas ilustra o appendChild
var str = '<fieldset><legend>Em XML</legend><p>Alguma coisa a dizer</p></fieldset>';
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(str,"text/xml");
//document.getElementById("resXML").appendChild(xmlDoc); NÃO FUNCIONA!
// É um exemplo simples, mas posso ter por exemplo uma coleção de formulários em um XML e pegar só que eu preciso adicionando por appendChild.
// Mas não funciona, então é o seguinte...
document.getElementById("resXML").innerHTML = (new XMLSerializer()).serializeToString(xmlDoc);

At the end, I have to serialize, which can even slow down a page, depending on the trouble.

On MDN Mozilla (Reference) it is as experimental, so my question is the following: this API will someday be compatible, we must invest in it, or there is a fundamental difference between the objects that we will always have in mind?

  • 1

    Try . appendchild(xmlDoc.documentElement)

  • Appears only the texts, without the tags - raw text. But worth the guess, already something...

  • Explains a little better how you want to use this XML in real life.

  • For example: I’m developing a website that uses multiple forms. I diagram them all on a separate page, identifying by ID or even by name. Then I load the page into a variable via xmlhttprequest and look for the form I need within this variable as needed - the forms are all available. Better than a page for each one that has to load every time it needs.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.