0

I have this XML problem that is bugging me today, and I can't find a solution because I don't know how to phrase it. My XML file looks like this:

<TEST>
  <ITEM_ID>0123
    <ITEM_SIZE>Medium</ITEM_SIZE>
    <ITEM_COLOR>Red</ITEM_COLOR>
  </ITEM_ID>
</TEST>

Everything works fine (i can find a random element, display or erase it) and can get the information I want, but the only thing that I cannot access the with myXML_XML.ITEM_ID[0], which shoud say "0123". I get nothing, no error, nothing. When I check myXML_XML.ITEM_ID[0].ITEM_SIZE (when put in a String Var, I get "Medium" ).

I thought perhaps the XML was misleading so I tried with another file:

<TEST>
      <ITEM_ID REF="0123">
        <ITEM_SIZE>Medium</ITEM_SIZE>
        <ITEM_COLOR>Red</ITEM_COLOR>
      </ITEM_ID>
    </TEST>

Which gave me the same results, when checking for myXML_XML.ITEM_ID[0].REF or myXML_XML.ITEM_ID[0].REF.text

Anyone kind enough to enlighten me?

1 Answer 1

1

Your first XML schema seems to be a little off, but valid nonetheless. You might want to try children() method of the nodes.

myXML_XML.ITEM_ID[0].children()[0] // This is the text node

In the other example, which is more readable in fact, you should use @ sign for accessing attributes

myXML_XML.ITEM_ID[0].@REF

Here is a complete example:

var test:XML = new XML(
    <foo>
        <bar id="4444">
            efghi
            <baz>1234</baz>
            <quux>6789</quux>
        </bar>
        <bar id="4445">
            zxcv
            <baz>4567</baz>
            <quux>9876</quux>
        </bar>
    </foo>
);

trace(test.bar[0].children()[0]); // efghi
trace(test.bar[1].@id); // 4445
Sign up to request clarification or add additional context in comments.

2 Comments

ah thank you ever so much, this was exactly what I was looking for. The reason the XML Schema is a bit weird is that by using this, I can manage to remove a node with: delete myXML_XML.ITEM_ID[0].parent().children() [myXML_XML.ITEM_ID[0].childIndex()}
but I also needed to record somewhere what I deleted (the ITEM_ID[0]) which I could not access. Thank you a million for making the @ clear to me in your example. Nik

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.