javascript and XML help

R

rbann11

Hi,

I have a script that contains an XML string that will define how the
page will be displayed.
The XML string contains custom tags, with attributes and html tags.
I will be replacing the custom tags with other html code.

My question is what is the best way to parse the XML string and can it
be done with DOM.
What is bothering me the most is the text before and after the custom
tags.

There is an example of what the XML string will look like:

<root>
Some Text and html tags
<block a="1 "b="2">
Some Text and html tags
<block>
Some Text and html tags
<tag1> </tag1>
<tag2> </tag2>
<tag3> </tag3>
<tag3> </tag4>
</block>
<block a="4">
Some Text and html tags
<tag1> </tag1>
<tag2> </tag2>
</block>
</block>
Some Text and html tags
</root>


Any code or ideas will be helpful.
Thanks in advance
Roger
 
M

Martin Honnen

My question is what is the best way to parse the XML string and can it
be done with DOM.

<root>
Some Text and html tags
<block a="1 "b="2">
^^^^^
That is not well-formed XML so any XML parser will give a parse error.
Some Text and html tags
<block>
Some Text and html tags
<tag1> </tag1>
<tag2> </tag2>
<tag3> </tag3>
<tag3> </tag4>
</block>
<block a="4">
Some Text and html tags
<tag1> </tag1>
<tag2> </tag2>
</block>
</block>
Some Text and html tags
</root>

If you want to parse XML from a string then look into
<http://www.faqts.com/knowledge_base/view.phtml/aid/15302/fid/616>

You need to be aware however that such an XML parsing will give you XML
DOM nodes, it does not help that some of them might have the same tag
name as HTML elements e.g.
<root>
<p><strong>Kibo</strong> is God.<p>
</root>
will parse root as well as p or strong into an XML DOM element node. And
those XML DOM element nodes do not become HTML DOM element nodes later
if you try to insert them into an HTML DOM document to have them rendered.
With browsers like Mozilla or Opera which have a common DOM
implementation beneath their HTML and XML parsers you could get XHTML
DOM nodes by using the proper XHTML namespace e.g.
<root>
<p xmlns="http://www.w3.org/1999/xhtml"><strong>Kibo</strong> is
God.<p>
</root>
that way the XML parser will create XHTML element nodes for those
elements in the XHTML namespace and these nodes can then be imported and
rendered in a HTML DOM document.
But IE has separate HTML and XML parsers and DOM implementations so its
XML parser will not do anything special with elements in the XHTML
namespace.
 
R

rbann11

Martin said:
^^^^^
That is not well-formed XML so any XML parser will give a parse error.


If you want to parse XML from a string then look into
<http://www.faqts.com/knowledge_base/view.phtml/aid/15302/fid/616>

You need to be aware however that such an XML parsing will give you XML
DOM nodes, it does not help that some of them might have the same tag
name as HTML elements e.g.
<root>
<p><strong>Kibo</strong> is God.<p>
</root>
will parse root as well as p or strong into an XML DOM element node. And
those XML DOM element nodes do not become HTML DOM element nodes later
if you try to insert them into an HTML DOM document to have them rendered.
With browsers like Mozilla or Opera which have a common DOM
implementation beneath their HTML and XML parsers you could get XHTML
DOM nodes by using the proper XHTML namespace e.g.
<root>
<p xmlns="http://www.w3.org/1999/xhtml"><strong>Kibo</strong> is
God.<p>
</root>
that way the XML parser will create XHTML element nodes for those
elements in the XHTML namespace and these nodes can then be imported and
rendered in a HTML DOM document.
But IE has separate HTML and XML parsers and DOM implementations so its
XML parser will not do anything special with elements in the XHTML
namespace.

Thanks for replying.
Actually, I misunderstood DOM. It does what I want it to.

Keep up the good work,

Roger
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top