【问题标题】:What is the recommended way of parsing an XML feed with multiple namespaces with ActionScript 3.0?使用 ActionScript 3.0 解析具有多个命名空间的 XML 提要的推荐方法是什么?
【发布时间】:2011-02-13 02:34:38
【问题描述】:

我在几个在线示例中看到了以下方法,但没有找到任何关于解析 XML 提要的推荐方法的文档。

方法一:

protected function xmlResponseHandler(event:ResultEvent):void
{
    var atom:Namespace = new Namespace("http://www.w3.org/2005/Atom");
    var microsoftData:Namespace = new Namespace("http://schemas.microsoft.com/ado/2007/08/dataservices");
    var microsoftMetadata:Namespace = new   Namespace("http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");

    var ac:ArrayCollection = new ArrayCollection();
    var keyValuePairs:KeyValuePair;
    var propertyList:XMLList = (event.result as XML)..atom::entry.atom::content.microsoftMetadata::properties;

    for each (var properties:XML in propertyList)
    {
        keyValuePairs = new KeyValuePair(properties.microsoftData::FieldLocation, properties.microsoftData::Locationid);
        ac.addItem(keyValuePairs);     
    } 

    cb.dataProvider = ac;
}

方法二:

protected function xmlResponseHandler(event:ResultEvent):void
{
    namespace atom = "http://www.w3.org/2005/Atom";
    namespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
    namespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";

    use namespace d;
    use namespace m;
    use namespace atom;

    var ac:ArrayCollection = new ArrayCollection();
    var keyValuePairs:KeyValuePair;
    var propertyList:XMLList = (event.result as XML)..entry.content.properties;

    for each (var properties:XML in propertyList)
    {
        keyValuePairs = new KeyValuePair(properties.FieldLocation, properties.Locationid);
        ac.addItem(keyValuePairs);     
    } 

    cb.dataProvider = ac;
}  

方法三:

protected function xmlResponseHandler(event:ResultEvent):void
{
    var ac:ArrayCollection = new ArrayCollection();
    var keyValuePairs:KeyValuePair;
    var propertyList:XMLList = (event.result as XML)..*::entry.*::content.*::properties;

    for each (var properties:XML in propertyList)
    {
        keyValuePairs = new KeyValuePair(properties.*::FieldLocation, properties.*::Locationid);
        ac.addItem(keyValuePairs);     
    } 

    cb.dataProvider = ac;
}  

示例 XML 提要:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<feed xml:base="http://www.test.com/Test/my.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
    <title type="text">Test_Locations</title>
    <id>http://www.test.com/test/my.svc/Test_Locations</id>
    <updated>2010-04-27T20:41:23Z</updated>
    <link rel="self" title="Test_Locations" href="Test_Locations" />
    <entry>
        <id>1</id>
        <title type="text"></title>
        <updated>2010-04-27T20:41:23Z</updated>
        <author>
            <name />
        </author>
        <link rel="edit" title="Test_Locations" href="http://www.test.com/id=1" />
        <category term="MySQLModel.Test_Locations" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
            <m:properties>
                <d:FieldLocation>Test Location</d:FieldLocation>
                <d:Locationid>test0129</d:Locationid>
            </m:properties>
        </content>
    </entry>
    <entry>
        <id>2</id>
        <title type="text"></title>
        <updated>2010-04-27T20:41:23Z</updated>
        <author>
            <name />
        </author>
        <link rel="edit" title="Test_Locations" href="http://www.test.com/id=2" />
        <category term="MySQLModel.Test_Locations" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
            <m:properties>
                <d:FieldLocation>Yet Another Test Location</d:FieldLocation>
                <d:Locationid>test25</d:Locationid>
            </m:properties>
        </content>
    </entry>
</feed>

【问题讨论】:

    标签: xml actionscript-3 parsing namespaces


    【解决方案1】:

    第三个完全违背了拥有命名空间的目的,忽略了它们。所以这是一个不。

    在前两种方法中,虽然它可能会导致几次额外的击键,但我更喜欢第一种方法,因为它明确说明了每个标识符所指的命名空间。

    我还必须补充一点,第二种方法对我来说是新的 - 还没有遇到过。

    【讨论】:

      猜你喜欢
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      • 2013-11-06
      • 2011-01-30
      • 1970-01-01
      • 2012-08-15
      相关资源
      最近更新 更多