0

I don't want to use jQuery for the following:

If I had the following XML

<parent>
    <a >some text
      <b propA="foo">some more text

      </b>
      and more text still
    </a>
   </parent>

and wanted to call something like

//pseudo... I pass in node 1 because node 0 is a useless empty text node...

getFlatXMLAsString(parent.childNodes[1])

and receive a string that looked like "<a>some text\n<b propA="foo">some more text\n\n</b>and more text still</a>"

Notice how it just converts the XML to a flat string, but includes the node's own opening and closing tags (and if that had attributes, it would have those in there as well).

Obviously I can do this manually but was hoping there's some built in mechanism that would prevent me re-inventing the wheel.

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236

2 Answers2

3

According to the answer posted here the cross-browser way to do this looks like

function outerHTML(node){
    // if IE, Chrome take the internal method otherwise build one
  return node.outerHTML || (
  function(n){
      var div = document.createElement('div'), h;
      div.appendChild( n.cloneNode(true) );
      h = div.innerHTML;
      div = null;
      return h;
  })(node);
  }

That is if I understand the question correctly.

Community
  • 1
  • 1
Danny
  • 7,368
  • 8
  • 46
  • 70
1

In Mozilla and Webkit browsers, you can use the XMLSerializer. Just do an object-test.

I'm not aware of what facilities are available in other browsers, but they might have one.

Worst case scenario, you'll have to write a function to do this by hand which walks all the nodes and writes to a string. This answer should get you started.

Community
  • 1
  • 1
Francis Avila
  • 31,233
  • 6
  • 58
  • 96
  • I don't think this does what I'm asking for. Notice that I'm looking to get a flattened version of the XML that is a child of the node I pass in, including all the brackets and attributes and everything. I'm not looking to strip out the internal nodes, I'm looking to get a flattened String representation of them. In my question I provide an example of what I'd expect to get back from my function, given the passed in node. – Yevgeny Simkin Dec 02 '11 at 18:50
  • Ah, you want to serialize the XML? Unfortunately this is going to depend on what environment you're in. Are you executing this JS in a browser? – Francis Avila Dec 02 '11 at 18:55
  • 1
    I am. And as you imagine, it has to be cross-browser. I'm starting to suspect that I should just roll my own... it's a really simple recursive method, in fact, I've written it before, years ago. Also, it looks like since IE treats XML the same as DOM, I may be able to just call for innerHMTL on the parent node to get what I want. I don't think that'll work in any non crap browser though :) – Yevgeny Simkin Dec 02 '11 at 18:57
  • Unfortunately there's no generic cross-browser way to do this. I've changed my answer to point you to what's out there. I imagine serialization from the DOM is easy in principle (recursive loops) but tricky in practice (entities, inline DTDs, odd corner cases). – Francis Avila Dec 02 '11 at 19:05