Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

how to open xml file into html page

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have created a drop-down box in an html page using SELECT and OPTION tags. there are 3 options A, B, and C... correspondingly there are 3 xml files exists in the same directory. i want to display a.xml file on selecting A option, b.xml file for B option. i tried my best to achieve it but cant do it yet. i want to do it only using html and javascript and no server side code.. please help me out.
 
Marshal
Posts: 67413
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vikas sharmaa wrote:i tried my best to achieve it but cant do it yet.



Without knowing what you've tried and how it failed, all we can do is guess.

 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the below code, i am trying to open first.xml on selecting first option in the html page. first.xml file is located in the current directory.



whats wrong in the above code. or, please tell me an alternative to open an xml file in the html page.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well you can not use document.write after the page loads. You code will only work in IE. And lastly the onchange event does not fire if you select the same option in the select element.

Eric

 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks eric. but my code is not working even after i remove document.write(xmlObj.xml) statement.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you added debug statements to see what is happening?

Eric
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes eric, i got the problem now. i added alert(xmlObj.xml) instead of document.write(xmlObj.xml) statement. the xml file is getting displayed into alert box. it means we cannot use document.write() inside onchange() event.

but now my query is what is the alternative of document.write() to display the contents on change event of the drop down box.
 
Bear Bibeault
Marshal
Posts: 67413
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can either use DOM manipulation, or set the innerHTML property of parent elements.
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear, i tried like this: added document.getElementById('result').innerHTML = xmldoc.documentElement.xml; inplace of alert(xmldoc.documentElement.xml);

and at the end create a result id as follows:




but am getting an unknown run time error

find below the complete code:



please tell me what is wrong in the above code.
 
Bear Bibeault
Marshal
Posts: 67413
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't insert random XML into the DOM tree; only HTML.

If you want to display the XML document as text within the HTML DOM, you'll need to replace the < and > characters with their HTML Entity equivalents (&lt; and &gt;);
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear,

i am trying to replace the < and > characters with their HTML Entity equivalents (< and >) using replace function as shown below:




but < and > characters are not getting replaced. i am getting the same alert message both the time.

please suggest.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic