Publish an xml string as table

dingir
dingir used Ask the Experts™
on
I have a result like

<?xml version="1.0" encoding="utf-8" ?>
- <Users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="xy">
  <ReturnOk>true</ReturnOk>
  <result>
     <set username='user1' signupdate='2007-03-01 18:36:05' xy='1' lastate='n/a' />
     <set username='user2' signupdate='2009-02-04 13:36:25' xy='1' lastdate='n/a' />
      <set username=user3' signupdate='2009-04-01 09:40:44' xy='1' lastdate='n/a' />
      <set username='user4' signupdate='2009-03-12 23:52:48' xy='1' lastdate='n/a' />
    </result>
  </Users>

..that I got onto client side after calling a webservice with ajax.
How do I use jquery to print that result to a table?

Each row (username) has a signupdate field, lastdate field, xy field.

Thank's in advanced,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Hi dingir,

Use regular expressions. To get familiar with them I recommend you a great text editor (as a beginner, this would improve some of the common tasks):

en.wikipedia.org/wiki/JEdit

www.jedit.org

¡Viva Mexico! Local First

General strategy:
- Erase
<?xml version="1.0" encoding="utf-8" ?>
- <Users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="xy">
  <ReturnOk>true</ReturnOk>

- Erase
  </Users>

- Find these:
  <result>
    </result>
- Replace for header and footer of a table

- Add a header (or read and add)
from <set username='user1' signupdate='2007-03-01 18:36:05' xy='1' lastate='n/a' />
to  headers (something like)
<td>user name</td>
<td>sign up date</td>
<td>xy</td>
<td>lastate</td>

- then replace (clean) each row

Also you can opt to translate your XML to JSON to table

Hope it helps you


Expert of the Year 2008
Top Expert 2008
Commented:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
$( function(){
	fetchPage("yourpage.xml");
});
 
function fetchPage(remoteURL){
	$.ajax({
		type: "GET",
		url: remoteURL,
		dataType: "xml",
		success: function(xml) {
			var rows="";
			$(xml).find("set").each(function(){
				rows+="<tr>";
				rows+="<td>"+$(this).attr("username")+"</td>";
				rows+="<td>"+$(this).attr("signupdate")+"</td>";
				rows+="<td>"+$(this).attr("xy")+"</td>";
				rows+="<td>"+$(this).attr("lastdate")+"</td>";
				rows+="</tr>";
			});
			
			$("#myTable > tbody").html(rows);
		});
 
}
</script>
</head>
<body>
<table id="myTable"><tbody></tbody></table>
</body>
</html>

Open in new window

Author

Commented:
Interesting.. thank's both of you.. that leaded my thinking in new directions..
Will check those up..

Commented:
Hi dingir and hielo

Thx hielo, I saw the light. I suggest dingir that use your solution because is more robust to the (FEW) changes that we suffer. Anyway, try using regexp you'll have fun.

Bye
Expert of the Year 2008
Top Expert 2008

Commented:
>>Hi dingir and hielo
Hi back

>>¡Viva Mexico!
Ah..NO. ¡Viva USA! :)

>>Thx hielo, I saw the light.
You are welcome :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial