0

I have a string:

private str:String = '
<!DOCTYPE html>
<html>
<body>
<div>TEST</div>
<span id="t1">T1 Content</span>
<span class="t2">T2 Content</span>
</body>
</html>';

I want to parse the string. Next, I get innerHTMl via Object, ID or Class.

E.g.:

Objects: Body, Div, Span
IDs: t1
Classes: t2

In PHP with the class, it's easy. But I could not create this build with Flex.

Thanks for help...

3
  • I think this question is too broad to be answered on this site. Maybe you should try to write your own HTML Parser in AS3 and/or try to convert the PHP Class you found then come back to us with specific errors you encountered. Commented Aug 29, 2012 at 18:05
  • 1
    If it would be XHTML, you could treat it as a regular XML object. But I think the first line of your example is already not compliant. Commented Aug 29, 2012 at 18:13
  • I believe as3 will ignore exclatives (<!...>) when creating native XML objects, so the first line should be fine, if not, you could always extract it with regex or string.replace() Commented Aug 29, 2012 at 18:34

1 Answer 1

1

Convert it to native XML in as3, then use e4x to parse the information required:

var xml:XML = XML(str);

//trace the content of any span with the id of "t1"
trace(xms..span.(attribute("id") == "t1"));

//trace the content of any span with the class t2
trace(xml..span.(attribute("class") == "t2"));

//trace the contents of the first div
trace(xml..div[0]);

For more on e4x, this is a good primer: http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=4

Sign up to request clarification or add additional context in comments.

3 Comments

I have a error: TypeError: Error #1085: The element type "link" must be terminated by the matching end-tag "</link>". docs.google.com/document/d/…
I see no link tag in your question, loading in complex html pages (like stackoverflow home page) will always be tricky because they don't always comply to XML standards. That error is because the link tags on stackoverflow don't have the trailing / on their tag, which makes the markup malformed causing that error.
If you're not in control of the html coming in, then you'd have to create some clean up function to make sure the markup is valid. You could also just search the string for the data you want and forget about converting it to xml.

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.