Struggling with XML of all things

Greetings,

I’m new to the world of HAXE and OpenFL My company asked me to look in to it as a means for broadening what we can do, and I don’t want to let them down as this appears to be a very promising tool. Until now I’ve mostly worked in HTML5, CSS, and AS3.

So my problem is that I’ve been trying to import an external XML file called cards.xml. I manged to get it imported, however when I trace out the data I get gibberish like this:

Main.hx:34: {
nodeType : 6, 
children : [{
		nodeType : 5, 
		children : [], 
		attributeMap : {
			h : {

			}
		}, 
		nodeValue : xml version="1.0" encoding="utf-8" , 
		parent : {
			nodeType : 6, 
			children : [<...>,<...>,<...>], 
			attributeMap : {
				h : <...>
			}
		}
	},{

By gibberish I mean stuff that isn’t useful for display. So I attempted to parse the data, and this is where I am struggling. My structure should look like this:

<data>
  <card id="1">
	<artist></artist>
	<title></title>
	<date></date>
	<image></image>
  </card>

With multiple card nodes following the above.

Can somebody point me to a guide on XML and how to interact with nodes and children? All my attempts produce little results and many errors. As it is I’ve found very few resources on this subject aside from creating XML (which I do not wish to do). I simply want to be able to target my nodes and loop through them.

Any help will be appreciated.

Hello! You probably are looking for the haxe.xml.Fast API (which in Haxe 4 previews is named haxe.xml.Access instead)

Although it is not a full E4X XML implementation, it has been more than enough for my needs

Here’s an example based on the Haxe documentation and W3Schools:

XML

<?xml version="1.0" encoding="UTF-8"?>
<note id="100">
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

Haxe

import haxe.xml.Fast;
import openfl.utils.Assets;

...

var data = Xml.parse (Assets.getText ("assets/note.xml"));
var xml = new Fast (data.firstElement ());

var id = (xml.has.id ? xml.att.id : 0);
var to = xml.node.to.innerData;
var from = xml.node.from.innerData;
var heading = (xml.hasNode.heading ? xml.node.heading.innerData : "");
var body = xml.node.body.innerData;

trace (id, to, from, heading, body);

You might also want innerHTML instead of innerData.

https://api.haxe.org/haxe/xml/Fast.html

Thanks for the reply!

I plugged the fields for my XML into what you provided, but I’m getting an odd error with this line:

var id = (xml.has.id ? xml.att.id : 0);

My XML is similar in structure to yours:

<card id="001">
    <artist>Grant Wood</artist>
    <title>American Gothic</title>
    <date>1930</date>
    <image>american-gothic.jpg</image>
</card>

So I do not see why it would give me this error message:

src/Main.hx:33: characters 38-39 : Int should be String

It’s clearly seeing the id value as a String “001” rather than the integer 1. I’ve tried to convert it (such as with Number() ), but HaxeDevelop keeps telling me those methods don’t work: Unknown Identifier : Number.

Any thoughts? And Thanks again!

EDIT:

std.parseInt() Is what I needed. I was having trouble finding that, but find it I did.

Thanks again!

Oops! Sorry, that was an issue with my code. It should have been "0" as a default value, or like you said, Std.parseInt or Std.parseFloat depending on the data type.

I usually don’t do it as a one-liner like that, but I wanted to help show that has is a valuable way to check if an attribute exists, which is super-common :slight_smile: