— Forest and the Trees

Archive
XML

Now I can copy and paste this code instead of having to remember a.name().toString();

Anyway – this takes
var x : XML =
<node name=’Doug’ hadAGoodTimeSleddingOnSunday = ‘true’/>
to
o.name = “Doug”;
o.hadAGoodTimeSleddingOnSunday = “true” //yup you’d want to type that as a Boolean.

var o : Object = {};
var atts : XMLList = x.attributes();
for each (var a : XML in atts) {
var propName : String = a.name().toString();
o[propName] = a.toXMLString();
}

Read More

File this under either RTFM or “can I get that hour of my life back”, maybe both. To trace xml that is simple (e.g., one node), you need to use toXMLString.

public var myXML:XML =<root>
<item id=’1′/>
<item id=’2′/>
<item id=’2′/></root>

public function init(): void {
trace(myXML);
trace(‘*************@mID_n==2**************’);
trace(myXML.item.(@id==2));//traces 2 nodes
trace(‘************@mID_n==1***************’);
trace(myXML.item.(@id==1));//traces nothing
trace(‘***************************’);
trace(myXML.item.(@id==1).toXMLString());//traces the node
}

Read More

Sometimes I need to url-encode some text for a static xml file. And sometimes I need to know the millisecond value of a date (converting millisecond values to dates is the fastest way to create date objects from XML in Flash. Get millisecond values in Flash by using millisecondValue=new Date().getTime()). And sometimes I make widgets to do this for me and then forget about them.

Read More

I had to convert an Excel file to XML so that I could load the data into Flash. I’m always excited when I get to use Microsoft Office – so this was a real treat. Actually, it’s not too bad – but, there was one major headache.

You need Excel 2003 for this to work. (I’m using Windows)

Excel 2003 has a couple ways to export XML. You can use “Save As -> XML Spreadsheet”. But, that gives you a ridiculous file with lots more info that you want (formatting info, etc.). The better way to do it is to write an xsd (xml schema) and load that into the spreadsheet. Here’s a sample xsd defining a repeating node with a bunch of attributes.

So, save that as sample.xsd. Then open your Excel spreadsheet. Choose “Data ->XML ->XML Source”. That opens the XML Source window. Click the button “XML Maps”. Then “Add”. Then load your schema (for some reason Excel calls xsds XML Maps).

In the XML Source window, your attribute names will show up. You can drag them onto the column names of your spreadsheet to map them. After mapping, choose “Save As” choose type “XML Data”. And voila, an XML file formatted to the xsd.

Here’s the rub – and the reason you need the “dontNeed” attributes (in the xsd above). For Excel files where the columns are not of equal length or have some empty cells, the XML file will not export if you do not map the columns in sequence from left to right. (I hope there is another way around this as well) So, start at the left and map every one of the columns, until you reach the last column you need to export. Then, “right click->remove element” the attributes in the XML Source window that you don’t need. Yes this is a kluge. But it does seem to work.

The error you get if you don’t do this is “A mapped element’s relationship with other elements cannot be preserved.” That’s a really helpful message.

Read More