  | | | XML to Object help | XML to Object help 2004-03-03 - By Merrill, Jason
Back I've used XML in the past and have done some simple things, but now I want to do something full-blown and am having troubles. I'm trying to convert my XML to an Object. I've tried out several .as parsers on sites like proto.layer51 .com and others, and haven't had much success. I've also started making my own parse function, but its slow going - - I can get to some data, but not others. I've also tried out the XML connection components but have been unsure how to collect the data from them and if those would work for my data. I found Barnden's XMLnitro, but couldn't find any instructions on how to use it.
The one .as class I found that seemed to have the most potential was heavy -rotation's XML -> Object (again) post on layer51. Here is my code for that prototype usage and the xml file. All traces return undefined. Any help is much appreciated! His XMLtoObject.as is at the bottom of this e-mail if that helps:
#include "XMLtoObject.as" x = new XML () x.ignoreWhite = 1 // this is essential! x.load ('m1Exam.xml') x.onLoad = function (){ pxml = this.toObject () }
trace (pxml) // returns undefined (supposed to return content of the first 'items' node) trace (pxml.moduleExam.question[0]) // returns undefined (supposed to return content of the second 'items' node's attribute 'attributeExample') trace (pxml.xmltest.item[1].thing) // returns undefined (supposed to return content of the second items child node (thing))
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----- Here is m1Exam.xml: (Ideally, it would be nice to acces this data like, "xmlO .moduleExam_2.question_1.option_3.content" which would be, "Leak detection equipment")
<?xml version="1.0" encoding="UTF-8 (See http://UTF-8.ora-code.com)" standalone="no"?> <moduleExam ID="2"> <question ID="1"> <statement>0. Identify the three items a Notification Form may include:< /statement> <options> <option correct="yes">Facility name</option> <option correct="no">Owner or operator's personal financial history< /option> <option correct="yes">Leak detection equipment</option> <option correct="no">Photographs of the owner or operator</option> <option correct="yes">Photographs of the facility</option> </options> </question> <question ID="2"> <statement>1. It is unnecessary to review a facility file before an inspection.</statement> <Options> <option>True 1</option> <option>False 1</option> </Options> </question> <question ID="3"> <statement>2. As part of UST regulations, facility owners or operators are required to provide pollution insurance and third party coverage.< /statement> <Options> <option>True 2</option> <option>False 2</option> </Options> </question> <question ID="4"> <statement>3. According to UST regulations, it is illegal to conduct an inspection without first notifying the owner.</statement> <Options> <option>True 3</option> <option>False 3</option> </Options> </question> </moduleExam>
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -- The XMLtoObject.as file:
XML.prototype.toObject = function(input_xml) { if (input_xml == null) { input_xml = this; } // make an empty object to attach properties and other objects to var returnObj = new Object(); var tempObj = new Object();
// loop throught the child nodes of the returned xml... for (var nodeIndex = 0; nodeIndex < input_xml.childNodes.length; nodeIndex++) { var currentNode = input_xml.childNodes[nodeIndex]; // for text nodes..... if (!(currentNode.firstChild.nodeValue == null && currentNode.hasChildNodes() )) { // anything thats not another nest of nodes, just make properties from them // creating a String object lets us add properties to it later var tempNode = new String(currentNode.firstChild.nodeValue); if (returnObj[currentNode.nodeName] == null) { returnObj[currentNode.nodeName] = tempNode; // this is used for attaching attributes later tempObj = returnObj[currentNode.nodeName]; } else { // make an array if it doesn't exist already if (returnObj[currentNode.nodeName][0] == null) { var tempObj = returnObj[currentNode.nodeName]; returnObj[currentNode.nodeName] = new Array(); returnObj[currentNode.nodeName][0] = tempObj; } returnObj[currentNode.nodeName].push(tempNode); // this is used for attaching attributes later tempObj = returnObj[currentNode.nodeName][returnObj[currentNode.nodeName] .length - 1]; }
} else {
// this is another nested object.... // if an object of this name doesnt exists... if (returnObj[currentNode.nodeName] == null) { returnObj[currentNode.nodeName] = this.toObject(currentNode); // this is used for attaching attributes later tempObj = returnObj[currentNode.nodeName]; } else { // make an array if it doesn't exist already if (returnObj[currentNode.nodeName][0] == null) { var tempObj = returnObj[currentNode.nodeName]; returnObj[currentNode.nodeName] = new Array(); returnObj[currentNode.nodeName][0] = tempObj; } returnObj[currentNode.nodeName].push(this.toObject(currentNode)); // this is used for attaching attributes later tempObj = returnObj[currentNode.nodeName][returnObj[currentNode.nodeName] .length - 1]; }
} tempObj.attributes = new Object(); var attributeCounter = 0; for (var a in currentNode.attributes) { attributeCounter++ tempObj.attributes[a] = currentNode.attributes[a]; } if (attributeCounter == 0) { tempObj.attributes = null; } }
// return the new object return returnObj;
}
Jason Merrill ICF Consulting Government e-Learning Solutions icfconsulting.com
NOTICE: This message is for the designated recipient only and may contain privileged or confidential information. If you have received it in error, please notify the sender immediately and delete the original. Any other use of this e-mail by you is prohibited.
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ------ Supported by Fig Leaf Software -- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ------ Lower Prices for Certified Training! Check out new lower prices for Certified Macromedia Training from Fig Leaf Software. Expand your skill set with courses in ColdFusion, Flash, Rich Internet Applications and .NET in the new year. Fig Leaf Software provides the highest caliber instruction at our training centers in Washington D.C., Atlanta, Chicago, Baltimore, Northern Virginia, or on-site at your location.
Get the details at http://training.figleaf.com/ -- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -- To unsubscribe, e-mail: flashnewbie-unsubscribe@(protected) For additional commands, e-mail: flashnewbie-help@(protected)
|
|
 |