Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Converting XML elements to JavaScript Array elements.

From: "Anthony Jones" <Ant@------------.--->
To: NULL
Date: 7/8/2006 12:39:00 PM


<pannbu79@g...> wrote in message
news:1152286214.296511.148210@s......
> Hi,
>
> I need to convert an XML file to a JS file,
>
> 1. Can I use <xsl:output method="text">?
>
> 2. I am suppose to convert the following pattern of XML to JavaScript
> file like the one given below.
> Input XML format:
> <links>
> <section1>
> <name>SearchEngines</name>
> <url>www.searchengines.com</url>
> <section2>
> <name>Google</name>
> <url>www.google.com</url>
> <section3>
> <name>Groups</name>
> <url>www.groups.google.com</url>
> </section3>
> <section3>
> <name>Mail</name>
> <url>www.gmail.com</url>
> </section3>
> </section2>
> <section2>
> <name>Yahoo</name>
> <url>www.yahoo.com</url>
> <section3>
> <name>Photos</name>
> <url>www.photos.yahoo.com</url>
> </section3>
> </section2>
> </section1> <!-- End of Section1  - I -->
> <section1>
> <name>Sample</name>
> <url>www.sample.com</url>
> <description>No section2 links are present for this.</description>
> </section1> <!-- End of Section1  - II -->
> <section1>
> <name>Tutorials</name>
> <url>www.tutorials.com</url>
> <section2>
> <name>XML</name>
> <url>www.tutorials.com/xml</url>
> <section3>
> <name>XSLT</name>
> <url>www.tutorials.com/xml/xslt</url>
> </section3>
> </section2>
>
> </section1> <!-- End of Section1  - III -->
> </links>
>
>
>
> Output JavaScript File: ( we need to make the values of the elements as
> arrays, naming for Each section's array has to be like it is given
> here.)
>
> //Section1 Entries
> Array1 = [
> [170, 120      // Few Hardcoded values
> ],
> ["SearchEngines","www.searchengines.com",1,0,1], // few values are
> hardcoded here 1,0,1
> ["Sample","www.sample.com",1,0,1],
> ["Tutorials","www.tutorials.com",1,0,1],
> ]
>
> //Section2 entries of Section1 I
> Array1_1 = [
> [],
> ["Google","www.google.com",1,0,0], // few values are hardcoded here
> 1,0,0
> ["Yahoo","www.yahoo.com",1,0,0],
> ]
>
> //Section2 entries of Section1 II - when description is present for
> Section 1, it should be given as section 2 link as well
> Array1_2 = [
> [],
> ["No section2 links are present for this.","www.sample.com",1,0,0],
> ]
>
> //Section2 entries of Section1 III
> Array1_3 = [
> [],
> ["XML","www.tutorials.com/xml",1,0,0],
> ]
>
> // Section 3 entries
>
> Array1_1_1 = [
> [],
> ["Groups","www.groups.google.com",1,0,0], // few values are hardcoded
> here 1,0,0
> ["Mail","www.gmail.com",1,0,0],
> ]
>
>
> Kindly let me know, how to do this kind of conversion from XML to
> JavaScript
>

This may be possible but have you heard of JSON (if not google it)

I think a better output would be a mixture of objects and arrays in a
heirarchy.

Eg.

I'm sure a better solution to:-

["Groups","www.groups.google.com",1,0,0], // few values are hardcoded

Would be:-

{name: 'Groups', url: 'www.groups.google.com', valuename: 1,
anothervaluename: 0,... }

Ultimately your Javascript code that actually uses this stuff will be way
more readable.  Also you won't need to create a mad variable names like
array_1_1_1.  Instead you'd have a single root variable holding a nested set
of arrays so accessing stuff will look like this:-

sections[0].sections[0].sections[0].url

BTW Section1, Section2, Section3 aren't good XML element names.  They should
all simply be called Section.

Let me know if you prefer to persue this approach and I'll help you develop
the XSL.  (I've always wanted a stab at XML to JSON but never got round to
it. Seen an example or two on the Web but they are too generic.)


> Thank you,
>
> Regards,
> Annbu P
>




transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent