Hi guys,
I am new to Ajax, xml and javascript.
I want to know how can I retrieve data from xml and display in the html page?
please help me out.
suppose my xml file is customer.xml the code is below: - <CUSTOMER>
-
-
<CUSTOMERID>1111</CUSTOMERID>
-
-
<NAME>
-
<FNAME>Antony</FNAME>
-
<MNAME>Federrer</MNAME>
-
<LNAME>Christine</LNAME>
-
</NAME>
-
-
<CONTACT>
-
<emailid>abc@yahoo.com</emailid>
-
<alt_emailid>def@yahoo.com</alt_emailid>
-
<contact_phone>0123456789</contact_phone>
-
<alt_phone>1234567890</alt_phone>
-
<alt_phone2>7894561230</alt_phone2>
-
<country></country>
-
</CONTACT>
-
-
</CUSTOMER>
-
---------------------------
I want to use javascript and Ajax only. I want to display in the html page.
Thanks.
34 2510
Welcome to Bytes!
See this example. You can also load an XML file without using Ajax, e.g. see this link. If you don't understand something or you can't adapt it to your needs, come back and ask.
PS. please use code tags when posting code. Thanks!
[quote=acoder]Welcome to Bytes!
Hi,
Thanks for your reply.
I saw the example in wschools.com/
In the example they are retrieving the records from the xml file. But here I have only one record.How can I do that?????
Please could you send me the code. Totally I am new to javascript and Ajax.......
Hope you will help me out.
Thanks
Whether you have one record or many records, the code would be the same or similar. Show what you've tried.
This code I tried to get records from the xml file.
I have a textfield tf1. In that text I will enter a customer id and when I click on customer id , it has to check that textfield with the customerid tag in the customerinfo.xml file, if it matches it should display all the information of the customer else it should display alert message customerid not found.
I have two files customerinfo.xml file and customerinf.html
customerinfo.xml - <CUSTOMER>
-
-
<CUSTOMERID>1111</CUSTOMERID>
-
-
<NAME>
-
<FNAME>Antony</FNAME>
-
<MNAME>Heff</MNAME>
-
<LNAME>huj</LNAME>
-
</NAME>
-
-
<CONTACT>
-
<EMAILID>abc@yahoo.com</EMAILID>
-
<ALT_EMAILID>cdfvr@gmail.com</ALT_EMAILID>
-
<CONTACT_PHONE>1234567895</CONTACT_PHONE>
-
<ALT_PHONE>42586934552</ALT_PHONE>
-
<ALT_PHONE2>7894561230</ALT_PHONE2>
-
<ADD_LINE1>gthenn</ADD_LINE1>
-
<ADD_LINE2>eeee</ADD_LINE2>
-
<ADD_LINE3>vbccb</ADD_LINE3>
-
<CITY>Bangalore</CITY>
-
<ZIP>825021</ZIP>
-
<COUNTRY>India</COUNTRY>
-
</CONTACT>
-
-
<PHOTOS>waterlily.GIF</PHOTOS>
-
-
-
</CUSTOMER>
-
customerinf.html file
[HTML]<html>
<form name="ff">
<head>
<title>CustomerInfo</title>
</head>
<script type="text/javascript">[/HTML]
[HTML] </script>
</head>
<body bgcolor="lightgreen">
<center>
<br><br>
<div id="copy">
<b>Enter Customer ID<input type="text" name="tf1" ><br><br>
<button onclick="loadXMLDoc('customerinfo.xml')">Get Customer Info</button></b>
</div>
</center>
</body>
</form>
</html>
[/HTML]
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Can any one tell me where did I go wrong.
So what's working and what's not working in your code?
Please use code tags when posting code. See How to Ask a Question.
With out this if condition in onResponse method, I am able to retrieve data from xml.
if(ff.tf1.value == y[0].firstChild.nodeValue)
{
alert("Strings are Equal");
}
else
{
alert("Strings are Not Equal");
}
But I want to retrieve based on the textfield value. Validaton I am unable to do it
Please can one give me the code for this.
First problem: the form should be in the body, so move the form opening tag to inside the body. Secondly, when referring to the form "ff", use document.forms["ff"] or document.ff, not just ff globally.
[HTML]<html>
<head>
<title>CustomerInfo</title>
</head>
<script type="text/javascript">
[/HTML] - var xmlhttp;
-
var xmlObj;
-
-
function loadXMLDoc(url)
-
{
-
xmlhttp=null;
-
-
if (window.XMLHttpRequest)
-
{// code for IE7, Firefox, Mozilla, etc.
-
xmlhttp=new XMLHttpRequest();
-
}
-
else if (window.ActiveXObject)
-
{// code for IE5, IE6
-
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
-
}
-
if (xmlhttp!=null)
-
{
-
xmlhttp.onreadystatechange=onResponse;
-
xmlhttp.open("GET",url,true);
-
xmlhttp.send(null);
-
}
-
else
-
{
-
alert("Your browser does not support XMLHTTP.");
-
}
-
}
-
-
function onResponse()
-
{
-
if(xmlhttp.readyState!=4) return;
-
if(xmlhttp.status!=200)
-
{
-
-
alert("Problem retrieving XML data");
-
return;
-
}
-
-
txt="<center><table border='3'><tr>";
-
-
txt = txt + "<td>Customer ID</td><td>";
-
-
y=xmlhttp.responseXML.documentElement.getElementsByTagName("CUSTOMERID");
[HTML] </script>
</head>
<body bgcolor="lightgreen">
<form name="ff">
<center>
<br><br>
<div id="copy">
<b>Enter Customer ID<input type="text" name="tf1" ><br><br>
<button onclick="loadXMLDoc('customerinfo.xml')">Get Customer Info</button></b>
</div>
</center>
</form>
</body>
</html>
[/HTML]
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
When I enter some value in the text field and click on submit button..again I am getting the same page again...
What is the problem?
if(document.ff.tf1.value == y[0].firstChild.nodeValue)
{
alert("Strings are Equal");
}
else
{
alert("Strings are Not Equal");
}
how to get the the value from the textfield????
I wrote document.ff.tf1.value is this right or not????
Please enclose your posted code in [code] tags (See How to Ask a Question).
This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.
Please use [code] tags in future.
MODERATOR
In your if statement, return if it doesn't match: - if (document.ff.tf1.value != y[0].firstChild.nodeValue) {
-
alert("Strings are not Equal");
-
return;
-
}
-
// the strings are equal so output the xml file contents
-
Still its returning back to the same page... I don't know what is the problem...
Could you please check the code once again and let me know what is the problem????...could you please give me the code??
The problem is that you're using a button element. Use an input button instead:
[html]<input type="button" ...>[/html]
I have written like this...
<input type="button" name="submit" value="submit" onClick="loadXMLDoc('customerinfo.xml')" >
Still I am not able to get the output
What is the error????
Please let me know. Can you give me the code please
Check the error console. Which browser are you testing on?
I am using Firefox....
There are no errors at all...
but it is returning back to the same page.
Please can you check the code once again and help me out
When using Ajax, you don't leave the page, so there's no concept of returning back to the same page.
In your first post, you said the XML file name was "customer.xml". In your code, you have "customerinfo.xml" as the file name. Check to make sure which is correct.
I used everywhere customerinfo.xml only....
Please can you go through the code once again and check what is the error???
I am using Eclipse and Tomcat server.
Is there any plugin for Ajax for Eclipse????I don't have idea...Please let me know
In your XML file, there's no PHOTOS tag, but you're checking for that in the JavaScript code. This would cause errors. Remove those lines or add the tag to the XML file.
I'm not sure about Eclipse plugins.
With out that if condition I am able to retrieve data from the xml.
if (document.ff.tf1.value != w[0].firstChild.nodeValue) {
alert("Strings are not Equal");
return;
}
If I keep that if condition then its not at all working... I got stucked off from the last days.
Don't know the problem, Please help me out.
Was it not y[0]? Have you changed y to w as the variable name?
I changed to w.
Please help me out
Can you post the latest version of the onResponse() function (in code tags)?
customerinfo.xml - (Code)text
-
-
<CUSTOMER>
-
-
<CUSTOMERID>1111</CUSTOMERID>
-
-
<NAME>
-
<FNAME>Venkat</FNAME>
-
<MNAME>Garu</MNAME>
-
<LNAME>Chalasani</LNAME>
-
</NAME>
-
-
<CONTACT>
-
<EMAILID>vchalasa@yahoo.com</EMAILID>
-
<ALT_EMAILID>venkat@samskruti.in</ALT_EMAILID>
-
<CONTACT_PHONE>00919845977499</CONTACT_PHONE>
-
<ALT_PHONE>0014084346578</ALT_PHONE>
-
<ALT_PHONE2>7894561230</ALT_PHONE2>
-
<ADD_LINE1>gthenn</ADD_LINE1>
-
<ADD_LINE2>eeee</ADD_LINE2>
-
<ADD_LINE3>vbccb</ADD_LINE3>
-
<CITY>Bangalore</CITY>
-
<ZIP>825021</ZIP>
-
<COUNTRY>India</COUNTRY>
-
</CONTACT>
-
-
<PHOTOS>waterlily.GIF</PHOTOS>
-
-
-
</CUSTOMER>
-
-
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
customerinf.xml
//////////////////////////////////////////////////////////////////////
This is my latest version of code.
Can you please check the code and give me the correct code.
Please help me out.
I tested your code and it works fine.
Are you sure there's no errors? I type in 1111 and click on the button and the form is replaced with the details of the customer from the XML file.
Finally It worked..Thanks for your help.
Well, that's good to hear. What was the problem in the end?
form name tag is in the wrong position.
Anyway I have got another problem now....
In my customerinfo.xml file I have this tag also after photos tag. -
-
<profile>
-
<pref>
-
<name=public>Antony </name>
-
<photos=community>jeep.gif </photos>
-
<family=private>house.gif </family>
-
</pref>
-
</profile>
-
-
////////////////////////////////////////////////
If I put the above code in the xml file and run, it does not work I don't know why??
Please help me out...
Have you modified the code to parse and display the added tags?
If I put that profile tag the program doesnot work at all.....
I didn't modify the code. Just if I put that tag it doesnot work at all
Perhaps the reason is that the XML is invalid. I think the <name=public> syntax may be causing the problem.
Still I didn't understand what is that tag for??
Please can you tell me briefly..........
Why is it invalid?
But in my task they asked me to add this also
Please help me out
Thanks for solving.....
What is that profile tag actually for?///
In xml file can we have same name tag???
I have name tag in my xml file
and also I have in profile tag I also have another name tag...
Can you tell me what about rules of naming tags in xml???
Thanks for solving.....
Glad that's solved.
What is that profile tag actually for?///
You posted it! Ask the person who added it to the XML file(!) Seriously, though, I think profile just means the profile of that customer, which has a prefs tag to include their preferences.
In xml file can we have same name tag???
I have name tag in my xml file
and also I have in profile tag I also have another name tag...
Can you tell me what about rules of naming tags in xml???
For these XML questions, I suggest you start a new thread in the XML forum.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: nospam |
last post by:
Reasons for a 3-tier achitecture for the WEB?
(NOTE: I said, WEB, NOT WINDOWS.
DON'T shoot your mouth off if you don't understand the difference.)
I hear only one reason and that's to switch a...
|
by: Steve |
last post by:
Can anyone tell me the preferred method for writing and
retrieving persistent information using .Net.
Specifically, I am referring to information that you used
to see in registry keys or .ini...
|
by: André Nogueira |
last post by:
Hi there guys!
I have one question... I'm doing a simple program in VB.Net 2003 that can
store my personal notes, my IE favourites, some pictures, etc.
I will also make an ASP.Net site (also in...
|
by: Sirplaya |
last post by:
I am retrieving images that I stored in SQL Server on my web pages in C#. I
have no problem with the images displaying, however, I am trying to wrap the
image with an <A HREF ..." and each time I...
|
by: Cerebral Believer |
last post by:
Hi folks,
I am creating a site in FrontPage, and want to use PHP to validate a form I
have created, however I would like the return of the users input (which the
user reviews to check for...
|
by: rfinch |
last post by:
Very new to this but using the MS working with dynamics CRM 3.0 book to run
web application to retrieve lead records from CRM 3.0.
Have followed the book instructions on page 380-382.
But am...
|
by: bmallett |
last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options...
|
by: Sudhakar |
last post by:
i have a registration page called register.php if the data entered is
validated correctly i call a file called thankyou.php or else
validate.php
presently a user after seeing the url...
|
by: Don S |
last post by:
Hi All.
Is there a way in Javascript to retrieve the HTML text from a webpage,
without actually opening the web page? For example, specify a web page
(it's actually the reply to an HTTP GET),...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |