Welcome to WebmasterWorld Guest from

Forum Moderators: httpwebwitch

Message Too Old, No Replies

no xml data in asp page- how to troubleshoot?

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

New User

5+ Year Member

joined:July 28, 2008
posts: 5
votes: 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?

1:07 am on July 29, 2008 (gmt 0)

Moderator This Forum from CA 

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
votes: 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)

3:21 pm on July 29, 2008 (gmt 0)

New User

5+ Year Member

joined:July 28, 2008
votes: 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:

//connect to db


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";
if ($num) {


//create or open file

$Handle = fopen($File, 'w');

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

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

while ($i < $num) {


----------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);


$data = "</recordset>";
fwrite($Handle, $data);
}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' ?>
<TheName><![CDATA[John Smith]]></TheName>
<FirstAddr><![CDATA[100 Whatever way]]></FirstAddr>
<CityState><![CDATA[City, ST 12345]]></CityState>
<TheName><![CDATA[Jane Doe]]></TheName>
<FirstAddr><![CDATA[100 W. Somewhere]]></FirstAddr>
<SecondAddr><![CDATA[Apt. 13, 2nd Floor]]></SecondAddr>
<CityState><![CDATA[Nowheresville, ST 12345]]></CityState>

the old XML data that DOES work looks like this:

<?xml version="1.0" standalone="yes"?>
<TheName>John Smith</TheName>
<SecondAddr>106 Washington</SecondAddr>
<CityState>Somewhere, ST 12345</CityState>

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


instead of


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 = _
set R1ThirdAddrNode = _
set R1letterNode = _
set R1TheTheNameNode = _



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?


5:43 pm on July 29, 2008 (gmt 0)

Moderator This Forum from CA 

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
votes: 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

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!

8:29 pm on July 29, 2008 (gmt 0)

New User

5+ Year Member

joined:July 28, 2008
votes: 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.