65287

Access XML nodes with integer names

Question:

For my application, I make an HTTPRequest, and get back some XML served from a JSP. That XML has some (yes, I'm aware this is invalid/improper XML. If I can't find a bandaid, I will try to address that internally) nodes with integers as names, say <2> for example.

When I attempt to access it, using myXMLVariable.child("2"), it returns the third (index=2) XML node instead. I understand that this behavior is <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/XML.html#child%28%29" rel="nofollow">"correct"</a>. Is there any way to get around this behavior?

<hr />

Example

var myXML:String = "<response>" + "<place1>" + " <item>1</item>" + " <stuff>1</stuff>" + "</place1>" + "<2>" + " <item>1</item>" + " <stuff>1</stuff>" + "</2>" + "<place3>" + " <item>1</item>" + " <stuff>1</stuff>" + "</place3>" + "</response>"; protected function getParam():void { var xml:XML = new XML(myXML); Alert.show(xml.child("2")); //trace(xml.child("2")) }

xml.child("2") returns

<place3> ... </place3>

...when I want

<2> ... </2> <hr />

NOTE

<strong>I am aware this is invalid XML.</strong> I am looking for a workaround, a short term fix. There is a near-future release date, and this workaround will be removed and replaced with proper XML for the next version.

Answer1:

Use E4X search expression on XMLList.

trace(xml.children().(name() == "2").toXMLString()); <ol><li>Get all children</li> <li>Search for the name() you need.</li> </ol>

Answer2:

From the XML specification:

[Definition: A Name is an Nmtoken with a restricted set of initial characters.] Disallowed initial characters for Names include digits, diacritics, the full stop and the hyphen.

Your <2> tag does not have a valid name. You should not be surprised it doesn't work as expected.

<strong>EDIT</strong>

If there is no way to get around working with invalid documents like this, I would probably use a RegExp to replace the invalid tags with valid ones, prior to processing the result:

public function replaceNumericalXMLTagNames( input:String ):String { var reg:RegExp = /(\<\/?)([0-9]+)(\>)/g; return input.replace( reg, function():String { return arguments[1]+"num"+arguments[2]+arguments[3]; } ) ); }

Answer3:

I think actionscript is 'helping' you. The param for .child is an object and I'll bet that actionscript sees a number and converts it and uses it as an index. If it were me I'd fix the XML. That's going to haunt you later.

Answer4:

If you want a short-term fix, change your non-XML with its non-standard tags to standard XML with proper named tags. Then you'll be able to use standard XML tools to manipulate it, and you'll get your code working far faster as a result.

Recommend

  • UpdateProgress Not working when called thru javascript
  • Magento Redirect from Observer
  • react native router not working
  • How to redirect Error Page and perform Routes in Zend Framework 1.x
  • How to make nicEditor snaplet? (Several questions)
  • How to use JavaScript to determine whether a file exists in a directory?
  • Functions in global context
  • Play WS (2.2.1): post/put large request
  • FileReader+canvas image loading problem
  • What is Eclipse's Declaration View used for?
  • Counter field in MS Access, how to generate?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Sending data from AppleScript to FileMaker records
  • Deserializing XML into class C#
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • AT Commands to Send SMS not working in Windows 8.1
  • Compare two NSDates in iPhone
  • Is there a mandatory requirement to switch app.yaml?
  • File upload with ng-file-upload throwing error
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • using HTMLImports.whenReady not working in chrome
  • Authorize attributes not working in MVC 4
  • apache spark aggregate function using min value
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • Net Present Value in Excel for Grouped Recurring CF
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?