-
BradyWhite.net
N00b at XML, HELP!!! Null?
flash code:
Code:
myXML = new XML();
myXML.onLoad = convertXML;
sub1 = "Loading data...";
myXML.load("buyersTools.xml");
function convertXML() {
if(myXML.loaded) {
sub1="loaded";
sub2 = myXML.firstChild.firstChild.nodeValue;
}
}
xml code:
PHP Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<buyersTools>
<mortgage>Mortgage
<item1>Two Key Factors in Qualifying</item1>
<item2>Loans</item2>
<item3>APR</item3>
<item4>Mortgage Terminology</item4>
</mortgage>
<downPayment>Down Payment
<item1>What makes low down payment loans possible?</item1>
<item2>Down Payment Loans and Gifts</item2>
<item3>Grant that is never repaid by the Homebuyer!</item3>
</downPayment>
<creditRating>
<item1>Mortages and Credit Reports</item1>
<item2>ABCs of Mortgage Credit</item2>
<item3>Credit Guide Scoring<item3>
<item4>How to Correct Errors</item4>
<item5>Credit Profile</item5>
<item6>Credit Report Access></item6>
<item7>Credit Questions and Answers</item7>
<item8>FICO Scores</item8>
</creditRating>
<fha>FHA
<item1>FHA Mortgage Insurance</item1>
<item2>Streamline Refinancing for FHA Mortgages</item2>
<item3>Down Payment Gifts</item3>
<item4>Bankrupcy and Foreclosure</item4>
<item5>Refunds on FHA Loans</item5>
<item6>Single Family Mortgage Insurance</item6>
<item7>Single Family Rehab Mortgage Program</item7>
<item8>Property Improvement Loan Insurance</item8>
<item9>Energy Efficient Mortgages</item9>
<item10>HUD Reverse Mortgage Program</item10>
</fha>
<item1>Speed up the Mortgage Process</item1>
<item2>After the Mortgage Application</item2>
<item3>Mortgage Payment Calculator</item3>
</buyersTools>
Sub2 is undefined. Why? Is my XML not right? It should displya "Mortgage".
Help!
Last edited by Kac; 09-04-2003 at 05:06 PM.
-
ok ok, this is an easy one, its in your XML file. I think your problem is that the node you are trying to access is not set up correctly. u can only have one root node for example
<person> <---- root node
<name>Nigel</name> <----- firstChild
</person> <-end of file
ok? Well you have attempted to set up 2 root nodes. The second of which you have data like:
<buyersTools> <------correct
<mortgage>Mortgage <--------- wrong
........
and then at the bottom u try and close the first node (blah). This is not how things work. If you have to give blah information use the attribute way of doing things.
<mortgage info="rate">
and keep that info in there. Or if u have to save stuff to mortgage close it straight after. You are confusing your poor computer. I hope this helps
:/
kevin.beckett@future8.co.uk
-
Hi Kac,
You've got a few issues that need to be dealt with:
- you haven't used the .ignoreWhite property that tells the XML parser to ignore extra white-space between nodes (line feeds, tabs, spaces, etc).
- you've used the variable for your document (myXML) instead of the keyword 'this' inside your onLoad handler
- you have fallen for the issue with text nodes
If you were to add a line that sets ignoreWhite to true, you would almost be there...
Lets have a look at what you've asked Flash to do:
- look in your XML document for the first child (without ignoreWhite this would be the xml processing instruction (<?xml version="1.0"?>, with ignoreWhite this would be <buyersTools>
- assuming you're now on the buyersTools node, then from there you've asked for the first child (without ignoreWhite: a text node with the contents of "{cr}{lf}{tab}", with ignoreWhite: the <mortgage> node)
- assuming you're now on the mortgage node, then from there you've asked for the nodeValue property... no matter what, this is null.
Have a look at this page where it explains the nodeValue property. Note that it only has a value on text nodes (type 3), in the XML you provided that's the first child node of the mortgage node (NOTE: the mortgage node has what we call mixed content, you've got text nodes and element nodes mixed together as siblings, not a good idea).
To make life easier, I like to use some helper functions, have a look at these:
code:
XML.prototype.documentElement = function()
{
var xmlElement = this.firstChild;
while( xmlElement != null && xmlElement.nodeType != 1 )
{
xmlElement = xmlElement.nextSibling;
}
return xmlElement;
};
XMLNode.prototype.selectSingleNode = function( nodeName )
{
for( var i=0; i < this.childNodes.length; i++ )
{
if( this.childNodes[i].nodeName == nodeName )
{
return this.childNodes[i];
}
}
return null;
};
XMLNode.prototype.nodeText = function()
{
if( arguments.length = 1 && arguments[0] == true )
{
// normalize all text nodes
var strResult = "";
var xmlCursor = this.firstChild;
while( xmlCursor != null )
{
if( xmlCursor.nodeType == 3 )
{
strResult += xmlCursor.nodeValue + " ";
}
xmlCursor = xmlCursor.nextSibling;
}
return strResult;
} else {
// simple mode, just the firstChild
if( this.firstChild != null && this.firstChild.nodeType == 3 )
{
return this.firstChild.nodeValue;
} else {
return "";
}
}
};
Once you have these, your code can look like this (I've added some trace statements to show what's happening):
code:
myXML = new XML();
myXML.onLoad = convertXML;
//myXML.ignoreWhite = true;
sub1 = "Loading data...";
myXML.load("buyersTools.xml");
function convertXML( success ) {
if( success ) {
sub1="loaded";
var xmlTest = this.firstChild;
trace( 'type: ' + xmlTest.nodeType + ' name: ' + xmlTest.nodeName + ' value: ' + xmlTest.nodeValue );
xmlTest = this.documentElement();
trace( 'type: ' + xmlTest.nodeType + ' name: ' + xmlTest.nodeName + ' value: ' + xmlTest.nodeValue );
trace( '"' + this.firstChild.firstChild.nodeValue + '"' );
trace( '"' + this.documentElement().selectSingleNode('mortgage' ).firstChild.nodeValue + '"' );
trace( '"' + this.documentElement().selectSingleNode('mortgage' ).nodeText() + '"' );
trace( '"' + this.documentElement().selectSingleNode('mortgage' ).nodeText(true) + '"' );
sub2 = this.documentElement().selectSingleNode('mortgage' ).nodeText();
}
}
Now, if you ignore the extra trace information you've got an easy to follow statement: this.documentElement().selectSingleNode('mortgage' ).nodeText(). That statement will work regardless of Flash 5/6, and regardless of the ignoreWhite setting.
There is still one issue, the text you get back is actually "Mortgage{cr}{lf}{tab}{tab}". The text is the same regardless of the ignoreWhite setting! Why? Well, in mixed-content mode the whitespace between text nodes and other nodes is considered significant. That's one of the reasons why mixed-content is not a good idea.
Hope this helps!
Tim Walters
Senior Developer
XML Evangelist
"XML isn't a language, it's a way of life!"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|