homepage Welcome to WebmasterWorld Guest from 174.129.130.202
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
no xml data in asp page- how to troubleshoot?
michael_s




msg:3709770
 10:00 pm on Jul 28, 2008 (gmt 0)


I am an XML n00b in big way. But I'm learning more every day thanks to this great forum! Lots of helpful information.

This is a general question. I have an asp page that grabs values from an xml file. First, it displays all of the Names in a dropdown. You choose a Name and click submit, and it submits the values in the associated nodes for a given name.

Working with an XML file built a few years ago by someone else, the page works okay. The data has since moved to a database, and I wrote a php page that selects the data and prints in a file that LOOKS okay- just like the older XML file. But I guess they are not exactly the same.

There was one majot difference that I did not notice at first: Some street addresses are like "5th & Main and in the old XML file & was expressed as &." However, when I produced the XML using my php file, I printed them right out of the db into the php template. The XML wouldn't validate until I changed the & to &.

Here's the weird part. Except for the errors thanks to the ampersands, The page was loading fine, and even populated the dropdown populated with the Names from my new datasource. But once I solved the ampersand problem, the dropdown was no longer populated. Interestingly, if I go back and undo the ampersand workaround, the dropdown will still not populate!

My question is, how do I troubleshoot this? Is there a way to validate the XML in a verbose fashion? I can load the file in an XML editor like Cooktop and it seems to be fine. What should I look for?
Thanks.

 

httpwebwitch




msg:3709872
 1:07 am on Jul 29, 2008 (gmt 0)

"&" is a special character in XML and can't be used anywhere in the value of a node, because it indicates the start of an XML "entity", like < or >. So in most XML situations, & is escaped as &

But ampersands are only one possible problem when stuffing user-entered data into XML. Ask yourself: what would happen if someone entered their address as:
<address>15 <i>Main St.</address>

it would crash and choke because you'd end up with some badly-formed XML.

There is another way, a better way, which involves wrapping your data in a "CDATA" section. CDATA stands for "Character Data", and it's a very good - indeed preferred - way to deal with data that could contain any character including ampersands, angle-brackets, etc. CDATA is not parsed as part of the XML, whatever goes inside is handled like a plain string, and the parser doesn't go sniffing inside looking for XML tags or worrying about validity or well-formedness.

To create a CDATA section, enclose data in an element like this: <![CDATA[ your data here ]]>

This is valid:
<address>5th &amp; main</address>

this is also valid:
<address><![CDATA[5th & main]]></address>

then you don't have to worry about the ampersands (and other special characters)

michael_s




msg:3710316
 3:21 pm on Jul 29, 2008 (gmt 0)

I appreciate the advice, and have changed my script to enclose the database values in the CDATA syntax you've provided. Thanks.

However, the data still does not display.

I am totally confused. My XML file is (except for the CDATA enclosures) identical to the older XML file, except for some data updates. I can change datasources back and forth and it will display the old one just fine, but totally poo-poos mine.

There must be something about XML file structure I'm not taking into account. My xml-creating (php) script looks like this:

<?php
//connect to db

$userName="uName";
$password="pw";
$database="dbName";

mysql_connect("localhost", $userName, $password);
@mysql_select_db($database) or die( "Unable to select database");

//select all data

$query="SELECT * from table order by id";
$result=mysql_query($query);
$num=mysql_num_rows($result);
if ($num) {

mysql_close();

//create or open file

$File="fileName.xml";
$Handle = fopen($File, 'w');

$data = "<?xml version='1.0' standalone='yes' ?>\n";
fwrite($Handle, $data);

$data = "<recordset>\n";
fwrite($Handle, $data);

$i=0;
while ($i < $num) {

$ID=mysql_result($result,$i,"ID");
$letter=mysql_result($result,$i,"letter");
$TheName=mysql_result($result,$i,"TheName");
$FirstAddr=mysql_result($result,$i,"FirstAddr");
$SecondAddr=mysql_result($result,$i,"SecondAddr");
$ThirdAddr=mysql_result($result,$i,"ThirdAddr");
$CityState=mysql_result($result,$i,"CityState");

----------NOTE- you can't tell because web browsers trim the whitespace, but in my script <R1> is preceeded by two spaces, and the other lines are preceeded by four. Even though a web browser would strip it out, I'm writing to a file and it faithfully puts two and four spaces at the head of each line as I indicate.------------

$data = " <R1>\n";
fwrite($Handle, $data);

$data = " <TheName><![CDATA[$TheName]]></TheName>\n";
fwrite($Handle, $data);

$data = " <FirstAddr><![CDATA[$FirstAddr]]></FirstAddr>\n";
fwrite($Handle, $data);

$data = " <SecondAddr><![CDATA[$SecondAddr]]></SecondAddr>\n";
fwrite($Handle, $data);

$data = " <ThirdAddr><![CDATA[$ThirdAddr]]></ThirdAddr>\n";
fwrite($Handle, $data);

$data = " <CityState><![CDATA[$CityState]]></CityState>\n";
fwrite($Handle, $data);

$data = " <ID><![CDATA[$ID]]></ID>\n";
fwrite($Handle, $data);

$data = " <letter><![CDATA[$letter]]></letter>\n";
fwrite($Handle, $data);

$data = " </R1>\n" ;
fwrite($Handle, $data);

$i++;
}

$data = "</recordset>";
fwrite($Handle, $data);
fclose($Handle);
}else {
$data = "There was a problem.";
}
?>

It creates XML that looks like this. You have to pretend the indentions are there, they are identical in both files.

<?xml version='1.0' standalone='yes' ?>
<recordset>
<R1>
<TheName><![CDATA[John Smith]]></TheName>
<FirstAddr><![CDATA[100 Whatever way]]></FirstAddr>
<SecondAddr><![CDATA[]]></SecondAddr>
<ThirdAddr><![CDATA[]]></ThirdAddr>
<CityState><![CDATA[City, ST 12345]]></CityState>
<ID><![CDATA[0]]></ID>
<letter><![CDATA[S]]></letter>
</R1>
<R1>
<TheName><![CDATA[Jane Doe]]></TheName>
<FirstAddr><![CDATA[100 W. Somewhere]]></FirstAddr>
<SecondAddr><![CDATA[Apt. 13, 2nd Floor]]></SecondAddr>
<ThirdAddr><![CDATA[]]></ThirdAddr>
<CityState><![CDATA[Nowheresville, ST 12345]]></CityState>
<ID><![CDATA[1]]></ID>
<letter><![CDATA[A]]></letter>
</R1>
</recordset>

the old XML data that DOES work looks like this:

<?xml version="1.0" standalone="yes"?>
<recordset>
<R1>
<TheName>John Smith</TheName>
<FirstAddr/>
<SecondAddr>106 Washington</SecondAddr>
<ThirdAddr/>
<CityState>Somewhere, ST 12345</CityState>
<ID>1</ID>
<letter>A</letter>
</R1>
</recordset>

Now I have just noticed mine has empty nodes that look like

<FirstAddr></FirstAddr>

instead of

<FirstAddr/>

Maybe that's part of the problem? Though the XML editors I've used seem to pick my empty nodes up just fine.

The ASP that displays the data looks like this:

<%
response.write"<form action='self.asp' method='post'><select name='TheName'><option>Select:</option>"
dim xmlDocument
dim xPath
dim R1
dim recordset
dim R1TheName
dim R1TheNameNode
dim R1FirstAddrNode
dim R1CityState
dim R1CityStateNode
dim R1FirstAddr
dim R1SecondAddr
dim R1SecondAddrNode
dim R1ThirdAddr
dim R1ThirdAddrNode
dim R1IDNode
dim R1letterNode
dim R1ID
dim R1letter

dim xhtml

xhtml = ""

set xmlDocument = getXml("filename.xml")

xPath = "//R1"
set recordset = xmlDocument.selectNodes(xPath)
for each R1 in recordset
R1TheName = R1.selectSingleNode("TheName").text
R1FirstAddr = R1.selectSingleNode("FirstAddr").text
R1SecondAddr = R1.selectSingleNode("SecondAddr").text
R1ThirdAddr = R1.selectSingleNode("ThirdAddr").text
R1CityState = R1.selectSingleNode("CityState").text
R1ID = R1.selectSingleNode("ID").text
R1letter = R1.selectSingleNode("letter").text

set R1FirstAddrNode = _
R1.selectSingleNode("FirstAddr")
set R1ThirdAddrNode = _
R1.selectSingleNode("ThirdAddr")
set R1letterNode = _
R1.selectSingleNode("letter")
set R1TheTheNameNode = _
R1.selectSingleNode("TheName")

response.write"<option>"&R1TheNameNode.text&"</option>"

next

response.write xhtml
response.write"</select><input type='submit' value='submit'></form>"

response.write"</div><div style='clear:both'> &#160; </div>"
%>
Any ideas? What should I be checking?

Thanks.

httpwebwitch




msg:3710451
 5:43 pm on Jul 29, 2008 (gmt 0)

pasted your XML into a validator, and it's well-formed.

self-closing or not-self-closing elements should be irrelevant (not true for some tags in HTML, but that's another topic)

The problem has *got* to be in the ASP that renders the options...
I'm not running anything on ASP (looks like VBScript) right now so I can't debug it hands-on

I don't know what this line accomplishes:
response.write xhtml

check some other things...
1) does your XML file have any whitespace before the <?xml declaration

2) also you're dimming a whole pile of variables when you're really only using the Name Node. Cleaning up unused code + variables will help with performance, but moreso can help to reveal problems... it just gets rid of noise and clutter

finally...
3) check your spelling here
[quote]set R1[b]The[/b]TheNameNode = _
R1.selectSingleNode("TheName") [/quote]

I strongly suspect you could accomplish this whole routine in 5 or 6 lines of code, instead of 50. Refactor and Simplify!

michael_s




msg:3710616
 8:29 pm on Jul 29, 2008 (gmt 0)

Thanks a lot for your help, httpwebwitch. I'm taking your advice about simplifying the code to heart. Funny thing, as I was making my way through the variables, I was checking the data to make sure I hadn't goofed up more node names, and I noticed that a couple of Names had an extra space between the first and last names. I deleted the spaces and voila, the page works perfectly!

So it wasn't the asp, or the structure of the XML... it was the data! Live and learn.

Thanks again.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved