views:

263

answers:

3

Hi, I am going to create an xml element in javascript to exchange data with server side. I found I can do it with document.createElement.But I do not know how to convert it to string. Is there any API in browser to make it easier? Or is there any js lib incudling this API?

Thanks in advance.

//Modification

I found that browser API XMLSerializer, it should be the right way to serialize to string.

A: 

There's a tagName property, and a attributes property as well:

var element = document.getElementById("wtv");
var openTag = "<"+element.tagName;
for (var i = 0; i < element.attributes.length; i++) {
    var attrib = element.attributes[i];
    openTag += " "+attrib.name + "=" + attrib.value;
}
openTag += ">";
alert(openTag);

See also http://stackoverflow.com/questions/828311/how-to-iterate-through-all-attributes-in-an-html-element (I did!)

To get the contents between the open and close tags you could probably use innerHTML if you don't want to iterate over all the child elements...

alert(element.innerHTML);

... and then get the close tag again with tagName.

var closeTag = "</"+element.tagName+">";
alert(closeTag);
LeguRi
I am afraid it takes sometimes to write the code which gets the entire markup.
xiao
If its XML it should all be in one root node, yes? If so, then you only have to do this for the root node - innerHTML is supported on all major browsers, and will give you the X(H)TML nested in the root node. (ie everything!)
LeguRi
Why bother looping over the element's attributes and building a string 'tag' from it? Just wrap the element in ANOTHER element, and take the innerHTML of that new parent element. Voila, instant workaround for outerHTML.
Marc B
Touché Mr. Marc B... touché
LeguRi
+1  A: 

The element outerHTML property (note: not supported by Firefox) returns the HTML of the entire element.

Example

<div id="new-element-1">Hello world.</div>

<script type="text/javascript"><!--

var element = document.getElementById("new-element-1");
var elementHtml = element.outerHTML;
// <div id="new-element-1">Hello world.</div>

--></script>

Similarly, you can use innerHTML to get the HTML contained within a given element, or innerText to get the text inside an element (sans HTML markup).

See Also

  1. outerHTML - Javascript Property
  2. Javascript Reference - Elements
Bauer
outerHTML is not supported in Firefox: http://www.quirksmode.org/dom/w3c_html.html#t05
Annie
But it seems I cannot get the inside text with the property outerHTML. I think what I really want is the HTML markup.
xiao
Javascript outerHTML actually works?
LeguRi
outerHTML property works fine in Chrome.
xiao
@Annie: Ah, of course. Edited the answer to indicate this.
Bauer
A: 

You can get the 'outer-html' by cloning the element, adding it to an empty,'offstage' container, and reading the container's innerHTML.

This example takes an optional second parameter.

Call document.getHTML(element, true) to include the element's descendents.

document.getHTML= function(who, deep){
    if(!who || !who.tagName) return '';
    var txt, ax, el= document.createElement("div");
    el.appendChild(who.cloneNode(false));
    txt= el.innerHTML;
    if(deep){
        ax= txt.indexOf('>')+1;
        txt= txt.substring(0, ax)+who.innerHTML+ txt.substring(ax);
    }
    el= null;
    return txt;
}
kennebec