Parse XML in Safari?

V

VUNETdotUS

How do I parse XML in Safari? I use E4X for Firefox. I can use IE-like
parser but replace .text with .textContent. However, I have problems
with xmlObj.childNodes(i).firstChild. I cannot use
xmlObj.childNodes(i).firstChild.textContent here:

xmlObj=xmlDoc.documentElement;
for(i=0;i<xmlObj.childNodes.length;i++)
{
alert(xmlObj.childNodes(i).text);
//alert(xmlObj.childNodes(i).textContent); for Safari
//alert(xmlObj.childNodes(i).firstChild.textContent); DOES NOT WORK
for Safari
}

Thanks
 
M

Martin Honnen

VUNETdotUS said:
How do I parse XML in Safari? I use E4X for Firefox. I can use IE-like
parser but replace .text with .textContent. However, I have problems
with xmlObj.childNodes(i).firstChild. I cannot use
xmlObj.childNodes(i).firstChild.textContent here:

xmlObj=xmlDoc.documentElement;
for(i=0;i<xmlObj.childNodes.length;i++)
{
alert(xmlObj.childNodes(i).text);
//alert(xmlObj.childNodes(i).textContent); for Safari
//alert(xmlObj.childNodes(i).firstChild.textContent); DOES NOT WORK
for Safari
}

You will need to show the relevant XML and tell us exactly which data
you want to access. A text node has a nodeValue property which you can
access e.g.
xmlObj.childNodes.firstChild.nodeValue
gives you a string with the text contents if firstChild is a text node.
If you want to access the text contents of an element (ala textContent
with DOM Level 3) then you need to loop through all children of the
element and concatenate the nodeValue of all text children and
recursively process the element children.
 
V

vunet

VUNETdotUS said:
How do I parse XML in Safari? I use E4X for Firefox. I can use IE-like
parser but replace .text with .textContent. However, I have problems
with xmlObj.childNodes(i).firstChild. I cannot use
xmlObj.childNodes(i).firstChild.textContent here:
xmlObj=xmlDoc.documentElement;
for(i=0;i<xmlObj.childNodes.length;i++)
{
alert(xmlObj.childNodes(i).text);
//alert(xmlObj.childNodes(i).textContent); for Safari
//alert(xmlObj.childNodes(i).firstChild.textContent); DOES NOT WORK
for Safari
}

You will need to show the relevant XML and tell us exactly which data
you want to access. A text node has a nodeValue property which you can
access e.g.
xmlObj.childNodes.firstChild.nodeValue
gives you a string with the text contents if firstChild is a text node.
If you want to access the text contents of an element (ala textContent
with DOM Level 3) then you need to loop through all children of the
element and concatenate the nodeValue of all text children and
recursively process the element children.


Thanks. I am in the middle of looping through children of the nodes
like:

........
var x = children.childNodes
if(x.tagName == "myTag1"){
alert(x.tagName) // <=====works ok for both IE &
Safari
alert(x.firstChild.text) // <=====works ok for IE only
alert(x.firstChild.nodeValue) // <=====does not work for Safari
alert(x.childNodes[0].textContent) // <=====works ok for Safari
only
}

How can I use firstChild for Safari?
 
D

David Mark

[snip]

Thanks. I am in the middle of looping through children of the nodes
like:

You forgot to post the XML.
.......
var x = children.childNodes


What does "children" refer to?
if(x.tagName == "myTag1"){
alert(x.tagName) // <=====works ok for both IE &
Safari
alert(x.firstChild.text) // <=====works ok for IE only
alert(x.firstChild.nodeValue) // <=====does not work for Safari
alert(x.childNodes[0].textContent) // <=====works ok for Safari
only

From your notes, I assume the first child is an element node, so the
nodeValue property won't help you. The textContent property is DOM
level 3, so I would avoid using it for compatibility reasons. As
Martin pointed out, your best bet is to loop through
x.firstChild.childNodes, concatenating the nodeValue properties of the
text nodes (nodeType == 3) and recursing to do the same for element
nodes (nodeType == 1.) You can detect when it is necessary to do this
by checking the type of x.firstChild.text (will be a string in IE.)
 
V

vunet

You forgot to post the XML.


<myTag1>
<myTag1a>text 1A</myTag1a>
<myTag1b>text 1B</myTag1b>
.......
var x = children.childNodes


What does "children" refer to?


children is a higher childNodes[N] collection

The textContent property is DOM
level 3, so I would avoid using it for compatibility reasons.

Really? So what other properties can be used for Safari to get a text
of a tag?

Martin pointed out, your best bet is to loop through
x.firstChild.childNodes, concatenating the nodeValue properties of the

x.firstChild.childNodes[0] is undefined in my case
text nodes (nodeType == 3) and recursing to do the same for element
nodes (nodeType == 1.) You can detect when it is necessary to do this
by checking the type of x.firstChild.text (will be a string in IE.)

x.firstChild.text works in IE only, not Safari

Thanks
 
D

David Mark

You forgot to post the XML.

<myTag1>
<myTag1a>text 1A</myTag1a>
<myTag1b>text 1B</myTag1b>
</myTag1>


.......
var x = children.childNodes

What does "children" refer to?

children is a higher childNodes[N] collection


What does that mean? Post the code that assigns a value to the
children variable.
Really? So what other properties can be used for Safari to get a text
of a tag?

As already mentioned, the nodeValue properties of the text nodes.
Martin pointed out, your best bet is to loop through
x.firstChild.childNodes, concatenating the nodeValue properties of the

x.firstChild.childNodes[0] is undefined in my case

x.childNodes[0] if x is a reference to myTag1A or myTag1B. Or
x.firstChild.
x.firstChild.text works in IE only, not Safari

That's what I said. To simplify this, post the code in its entirety.
It is hard to follow what you are doing by looking at pieces of code.
 
V

vunet

I'll post the XML soon. It is a bit complicated and on my intranet.
However, can I rephrase the questions?

If this is an XML:
<myTag1>
<myTag1a>text 1A</myTag1a>
<myTag1b>text 1B</myTag1b>
</myTag1>
How would I access text value in myTag1a, myTag1b in Safari browser?

Let's assume x.tageName = "myTag1"; //parent
You say I can use x.firstChild.nodeValue to access text inside of
"myTag1a"?
But I think that this does not work in Safari. My test showed me that
I can loop through x.childNodes to get text values like this:
var children = x.childNodes
x.childNodes[0].textContent //<-- "text 1A" but won't work if
x.firstChild.nodeValue is used.

Am I confused here or it seems to make sense? Thanks
 
T

Thomas 'PointedEars' Lahn

vunet said:
I'll post the XML soon.
It is a bit complicated and on my intranet.

In that case, please don't.
However, can I rephrase the questions?

If this is an XML:
<myTag1>
<myTag1a>text 1A</myTag1a>
<myTag1b>text 1B</myTag1b>
</myTag1>
How would I access text value in myTag1a, myTag1b in Safari browser?

Let's assume x.tageName = "myTag1"; //parent

Let `d' be a reference to the Document object of `x' as in

var p = new DOMParser();
var d = p.parseFromString([
'<myTag1>',
' <myTag1a>text 1A</myTag1a>',
' <myTag1b>text 1B</myTag1b>',
'</myTag1>'
].join("\n"),
"text/xml");

Then:

var result = d.evaluate('//myTag1a[1]', x, null, 7, null);
if (result && isMethodType(typeof result.snapshotItem)
&& result.snapshotItem)
{
var o = result.snapshotItem(0);
if (o)
{
window.alert(o.textContent);
}
}

WFM in Safari 3.0.4 Beta ["Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE)
AppleWebKit/523.12.9 (KHTML, like Gecko) Version/3.0 Safari/523.12.9"].
You say I can use x.firstChild.nodeValue to access text inside of
"myTag1a"?

Yes, you can.
But I think that this does not work in Safari.

But it does. However, you have to take into account that whitespace between
tags (not only between elements) constitutes a whitespace text node, and
that longer text content can be splitted into several text nodes.
Therefore, I prefer XPath expressions and the `textContent' property where
available.


PointedEars
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top