innerXML

P

Philipp Lenssen

Why isn't there a way in standard DOM to access all XML of a node?
(Similar to what outerHTML and innerHTML can do on Firefox or Internet
Explorer when accessing HTML.)
Or am I missing something?
 
M

Martin Honnen

Philipp said:
Why isn't there a way in standard DOM to access all XML of a node?
(Similar to what outerHTML and innerHTML can do on Firefox or Internet
Explorer when accessing HTML.)
Or am I missing something?

Well outerXml, that is the serialization of the node itself can
certainly be achieved with W3C DOM Level 3 Load and Save e.g. with Opera
7.60 Preview you can do

function getOuterXml (node) {
if (node.ownerDocument && node.ownerDocument.implementation &&
document.implementation.createLSSerializer &&
(serializer =
node.ownerDocument.implementation.createLSSerializer()))
{
return serializer.writeToString(node);
}
}

getOuterXml(someNode)

I think Java 1.5 (also named Java 5.0) also implements DOM Level 3 Load
and Save so there you are also able to serialize a node with standard
DOM ways:
http://java.sun.com/j2se/1.5.0/docs/api/index.html

And innerXml only requires you to serialize all child nodes and
concatenate the results.
 
P

Philipp Lenssen

Martin said:
And innerXml only requires you to serialize all child nodes and
concatenate the results.

I did that once in Visual Basic. Of course it takes some efforts, and I
wonder, really wonder -- and I'm not the only one, my colleagues wonder
as well :) -- why this is not in the basic DOM (innerXML and outerXML).

I'm using PHP5. Any code samples are very welcome. Both Outer and
InnerXML would be great.
 
M

Martin Honnen

Philipp Lenssen wrote:

I'm using PHP5. Any code samples are very welcome. Both Outer and
InnerXML would be great.

According to the docs all you need for "OuterXml" is
xmlDocument->saveXML(node)
as documented here
http://www.php.net/manual/en/function.dom-domdocument-savexml.php
For "InnerXml" you would then need to concatenate the OuterXml of all
child nodes:
function getInnerXml ($node) {
$innerXml = '';
$xmlDoc = $node->ownerDocument;
for ($i = 0; $i < $node->childNodes->length; $i++) {
$innerXml .= $xmlDoc->saveXML($node->childNodes[$i]);
}
return $innerXml;
}

All untested as I currently here do not have PHP 5 installed.
 
M

Martin Honnen

Martin Honnen wrote:

For "InnerXml" you would then need to concatenate the OuterXml of all
child nodes:
function getInnerXml ($node) {
$innerXml = '';
$xmlDoc = $node->ownerDocument;
for ($i = 0; $i < $node->childNodes->length; $i++) {
$innerXml .= $xmlDoc->saveXML($node->childNodes[$i]);

The square bracket access to childNodes doesn't work so you need

function getInnerXml ($node) {
$innerXml = '';
$xmlDoc = $node->ownerDocument;
for ($i = 0; $i < $node->childNodes->length; $i++) {
$innerXml .= $xmlDoc->saveXML($node->childNodes->item($i));
}
return $innerXml;
}

but then it works fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top