473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Xml content parsed in JS

Hello,

My script gets the content of an XML file in a string of characters thanks
to Ajax.
I'd like to transform the XML content to an HTML table.
Is there an easy way to do so without coding a whole XML parser in JS ?

Thanks in advance,

--
pierre
Aug 19 '06 #1
12 1729
pierre wrote:

My script gets the content of an XML file in a string of characters thanks
to Ajax.
I'd like to transform the XML content to an HTML table.
Is there an easy way to do so without coding a whole XML parser in JS ?
That shouldn't be a problem, but the code depends on the structure of
the XML file. Could you post it ?

--
Bart

Aug 19 '06 #2


pierre wrote:

My script gets the content of an XML file in a string of characters thanks
to Ajax.
I'd like to transform the XML content to an HTML table.
Is there an easy way to do so without coding a whole XML parser in JS ?
Ajax? Well that does not really tell what code and APIs you use. If you
are using XMLHttpRequest then it already has XML parsing support for the
response the server sends built in, just make sure the server sends the
answer with an XML MIME type as the Content-Type header e.g.
Content-Type: application/xml
or
Content-Type: text/xml
and XMLHttpRequest parses the response body as XML and populates
requestObject.responseXML as an XML DOM document you can script with DOM
methods (DOM Level 2 Core and XML, some DOM Level 3 Core, DOM Level 3
XPath with Mozilla or with Opera 9, DOM Level 1 XML with IE/MSXML where
MSXML 3 and later support some MSXML specific stuff for XPath).

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 19 '06 #3
That shouldn't be a problem, but the code depends on the structure of
the XML file. Could you post it ?

--
Bart
This is my Xml content :

<MyData>
<Columns>
<Column>Id</Column>
<Column>Name</Column>
</Columns>
<Rows>
<Row>
<Id>1</Id>
<Name>Peter</Name>
</Row>
<Row>
<Id>2</Id>
<Name>John</Name>
</Row>
</Rows>
</MyData>

thanks in advance,

--
larry
Aug 20 '06 #4
Ajax? Well that does not really tell what code and APIs you use. If you
are using XMLHttpRequest then it already has XML parsing support for the
response the server sends built in, just make sure the server sends the
answer with an XML MIME type as the Content-Type header e.g.
Content-Type: application/xml
or
Content-Type: text/xml
and XMLHttpRequest parses the response body as XML and populates
requestObject.responseXML as an XML DOM document you can script with DOM
methods (DOM Level 2 Core and XML, some DOM Level 3 Core, DOM Level 3
XPath with Mozilla or with Opera 9, DOM Level 1 XML with IE/MSXML where
MSXML 3 and later support some MSXML specific stuff for XPath).

--

Martin Honnen
http://JavaScript.FAQTs.com/
In fact, I'm coding a web page in which the user can type a SQL query.
I send this query to a PHP script thanks to Ajax.
The PHP script sends XML content back.
The trouble is how to transform this XML content to a simple HTML table.

thanks,

--
pierre
Aug 20 '06 #5


pierre wrote:

In fact, I'm coding a web page in which the user can type a SQL query.
I send this query to a PHP script thanks to Ajax.
The PHP script sends XML content back.
The trouble is how to transform this XML content to a simple HTML table.
Well if you want a HTML table then certainly one solution is to have
your PHP script create the HTML table and not XML and send the HTML
table back to the client side script.

If your PHP script has to send XML then I have already explained that
XMLHttpRequest on the client does everything to parse the XML it
receives into a DOM document that you can navigate using DOM scripting.
So send your XML with HTTP response header
Content-Type: application/xml
use XMLHttpRequest on the client and traverse responseXML to extract the
XML data you need and build your HTML table with the DOM.
See
<http://www.faqts.com/knowledge_base/view.phtml/aid/6826/fid/616>

In some browsers (IE 6, Firefox/Mozilla, Opera 9) you can also run XSLT
transformations on responseXML thus if you know to write XSLT
stylesheets then you could write one to transform the XML to a HTML
table and run that transformation on the client and insert the result in
your HTML document.
For Mozilla's and Opera's XSLT processor API see here:
<http://developer.mozilla.org/en/docs/Using_the_Mozilla_JavaScript_interface_to_XSL_Tran sformations>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 20 '06 #6
pierre wrote:
This is my Xml content :

<MyData>
<Columns>
<Column>Id</Column>
<Column>Name</Column>
</Columns>
<Rows>
<Row>
<Id>1</Id>
<Name>Peter</Name>
</Row>
<Row>
<Id>2</Id>
<Name>John</Name>
</Row>
</Rows>
</MyData>
Here's one way to display this in tabular format:

x = '<MyData ';
x += ' <Columns ';
x += ' <Column>Id</Column ';
x += ' <Column>Name</Column ';
x += ' </Columns ';
x += ' <Rows ';
x += ' <Row ';
x += ' <Id>1</Id ';
x += ' <Name>Peter</Name ';
x += ' </Row ';
x += ' <Row ';
x += ' <Id>2</Id ';
x += ' <Name>John</Name ';
x += ' </Row ';
x += ' </Rows ';
x += '</MyData ';

x = x.replace(/<MyData>/ig,'<table border="1">');
x = x.replace(/<\/MyData>/ig,'</table>');
x = x.replace(/<Columns>|<Row>/ig,'<tr>');
x = x.replace(/<\/Columns>|<\/Row>/ig,'</tr>');
x = x.replace(/<Id>|<Name>/ig,'<td>');
x = x.replace(/<\/Id>|<\/Name>/ig,'</td>');
x = x.replace(/<Column>/ig,'<th>');
x = x.replace(/<\/Column>/ig,'</th>');
x = x.replace(/<Rows>|<\/Rows>/ig,'');

document.writeln(x)

--
Bart

Aug 22 '06 #7
Use XSLT.

Aug 22 '06 #8
"Tom Cole" <tc****@gmail.coma écrit dans le message de news:
11**********************@i42g2000cwa.googlegroups. com...
Use XSLT.
I've looked in this direction... still, all browsers don't really seem to be
compliant with it (from what I quickly read).
Am I wrong ?

--
pierre
Aug 23 '06 #9
pierre wrote:
>Ajax? Well that does not really tell what code and APIs you use. If you
are using XMLHttpRequest then it already has XML parsing support for the
response the server sends built in, just make sure the server sends the
answer with an XML MIME type as the Content-Type header e.g.
Content-Type: application/xml
or
Content-Type: text/xml
and XMLHttpRequest parses the response body as XML and populates
requestObject.responseXML as an XML DOM document you can script with DOM
methods (DOM Level 2 Core and XML, some DOM Level 3 Core, DOM Level 3
XPath with Mozilla or with Opera 9, DOM Level 1 XML with IE/MSXML where
MSXML 3 and later support some MSXML specific stuff for XPath).

--

Martin Honnen
http://JavaScript.FAQTs.com/

In fact, I'm coding a web page in which the user can type a SQL query.
I send this query to a PHP script thanks to Ajax.
The PHP script sends XML content back.
The trouble is how to transform this XML content to a simple HTML table.

thanks,
I think that considering your data's schema, you would be better off
using JSON as the transport. JSON works easily in every browser that
supports XmlHttpRequest, unlike XML. Instead of messing with the DOM
tree of responseXML (as you would have to with XML), you can simply use
one line of code to turn your JSON data (from responseText) into an
array of Objects, and use much less code to generate your table using DOM.

For more information about JSON, go to <http://www.json.org- in
particular, check out the "PHP-JSON" link
(<http://www.aurore.net/projects/php-json/>), which provides a very fast
PHP extension that encodes any PHP variable, object, or array into a
JSON string with one call. Handy!

If you want to stick with XML, you may want to look at Google's
heavyweight "AJAXSLT" library: <http://goog-ajaxslt.sourceforge.net/-
I have never used it and am not vouching for it, but if you want to
reliably do XSLT from Javascript you're going to have to use a fairly
heavy library to do it.

Jeremy
Aug 23 '06 #10
I think that considering your data's schema, you would be better off
using JSON as the transport. JSON works easily in every browser that
supports XmlHttpRequest, unlike XML. Instead of messing with the DOM
tree of responseXML (as you would have to with XML), you can simply use
one line of code to turn your JSON data (from responseText) into an
array of Objects, and use much less code to generate your table using DOM.
I agree that JSON might be easier, but he's already got XML coming from
his server. If you've got the option to provide JSON text output, you
may want to consider it. It's no harder then pumping out an XML file.
If you want to stick with XML, you may want to look at Google's
heavyweight "AJAXSLT" library: <http://goog-ajaxslt.sourceforge.net/-
I have never used it and am not vouching for it, but if you want to
reliably do XSLT from Javascript you're going to have to use a fairly
heavy library to do it.
How heavyweight is XSLTProcessor (non-IE) or Microsoft.XMLDOM (IE)?
Doesn't seem that cumbersome to me. I do something similar (you'd have
to hack at this code a bit but for your purposes, but)...

if (typeof XSLTProcessor != 'undefined') {
var processor = new XSLTProcessor();
var request = getXMLHttpRequest();
if (request) {
request.open("GET", url, false);
request.send(null);
var xsl = request.responseXML;
processor.importStylesheet(xsl);
var fragment = processor.transformToFragment(value, document);
if (element)
element.appendChild(fragment);
}
else {
return;
}
}
else {
var processor = new ActiveXObject("Microsoft.XMLDOM");
if (processor) {
processor.async = false;
processor.load(url);
var xmlString = value.transformNode(processor);
if (element)
element.innerHTML = xmlString;
}
else {
return;
}
}

Aug 23 '06 #11

"Tom Cole" <tc****@gmail.coma écrit dans le message de news:
11*********************@m73g2000cwd.googlegroups.c om...
>I think that considering your data's schema, you would be better off
using JSON as the transport. JSON works easily in every browser that
supports XmlHttpRequest, unlike XML. Instead of messing with the DOM
tree of responseXML (as you would have to with XML), you can simply use
one line of code to turn your JSON data (from responseText) into an
array of Objects, and use much less code to generate your table using
DOM.

I agree that JSON might be easier, but he's already got XML coming from
his server. If you've got the option to provide JSON text output, you
may want to consider it. It's no harder then pumping out an XML file.
>If you want to stick with XML, you may want to look at Google's
heavyweight "AJAXSLT" library: <http://goog-ajaxslt.sourceforge.net/-
I have never used it and am not vouching for it, but if you want to
reliably do XSLT from Javascript you're going to have to use a fairly
heavy library to do it.

How heavyweight is XSLTProcessor (non-IE) or Microsoft.XMLDOM (IE)?
Doesn't seem that cumbersome to me. I do something similar (you'd have
to hack at this code a bit but for your purposes, but)...

if (typeof XSLTProcessor != 'undefined') {
var processor = new XSLTProcessor();
var request = getXMLHttpRequest();
if (request) {
request.open("GET", url, false);
request.send(null);
var xsl = request.responseXML;
processor.importStylesheet(xsl);
var fragment = processor.transformToFragment(value, document);
if (element)
element.appendChild(fragment);
}
else {
return;
}
}
else {
var processor = new ActiveXObject("Microsoft.XMLDOM");
if (processor) {
processor.async = false;
processor.load(url);
var xmlString = value.transformNode(processor);
if (element)
element.innerHTML = xmlString;
}
else {
return;
}
}
thank you all for your response.
i think i'll use json.

--
pierre
Aug 24 '06 #12


Jeremy wrote:

I think that considering your data's schema, you would be better off
using JSON as the transport. JSON works easily in every browser that
supports XmlHttpRequest, unlike XML.
Which browser supports _XML_HttpRequest but does not support _XML_ in
your view?
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 24 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
by: Harlan Messinger | last post by:
Why would anyone ever have expected a content-type META tag to be effective at all? Is it because someone was misled by the happenstance the letters of the alphabet, the digits, and the characters...
3
by: Headless | last post by:
Should linking generated content work? Example: span:before{content:"foobar"} <a href="foobar.htm"><span></span></a> I stumbled across this bit in the CSS2 spec: >Generated content does...
0
by: Jimmy Cerra | last post by:
I recently came up with a cool little stylesheet for definition lists. There is a small demostration for the impatient . I hope this helps someone. Here's how I did it. Definition lists are...
6
by: Alex | last post by:
Hi all. Well, I need some light in this simple thing I'm trying to do. I'm using the XMLHttpRequest to retrieve some data from a db via php script. The result is passed to a "results" array of...
9
by: wardy | last post by:
I'm trying to undestand the impact of using content negotiation when rendering my Web pages to various different browsers as I would like to use the XHTML Strict DOCTYPE declaration. Reading the...
12
by: Christian Roth | last post by:
Hello, I am merely asking this for my own understanding: Processing instruction's data part is not entity-aware, i.e. character and numercial entities are not resolved at parsing time. E.g., ...
7
by: kashaziz | last post by:
Hello, I am here after head-banging with this issue for several days. I am trying to parse a feed using JS and have stucked on the <content:encoded> tag. I have tried getElementsbyTagName but...
10
by: JJ | last post by:
I am needing to write a simple content management system for my web site. I've not done this before so if anyone has any link/info please could you mention them? One thing I am confused about:...
7
by: Cyberwolf | last post by:
Hi all I'm having a strange problem when upgrading the content management system eZ publish from PHP 4.4 to PHP 5.1. Some cache files related to i18n are included with the "include" function,...
6
by: Lord0 | last post by:
Hi there, How do I define in a schema that an element (<element>) may have any content i.e. text, other elements, partial elements, angle brackets etc? So all of the following would be valid:...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.