XML节点无法跟踪

时间:2015-11-20 13:51:21

标签: xml actionscript-3

Web服务通过SOAP返回XML,我正在尝试解析它。但是我无法到达节点。我完全跟踪XML,但节点无法追踪。

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <SbDrsRprRspnse xmlns="http://tempuri.org/">
            <SbDrsRprRslt>
                <SODSRprDt>
                    <Aktiv>false</Aktiv>
                    <Silindi>false</Silindi>
                    <Sira>0</Sira>
                    <Numara>IL1</Numara>
                    <AdSoyad>Maksim Tsygalha</AdSoyad>
                    <ToplamEtk>30</ToplamEtk>
                    <TamamEtk>6</TamamEtk>
                    <Durum>20</Durum>
                    <Units>
                        <SbOgrtDSUnitRDt>
                            <Aktif>false</Aktif>
                            <Silindi>false</Silindi>
                            <UnitsAdi>Ünite 3</UnitsAdi>
                            <EtkTop>30</EtkTop>
                            <TamamEtk>6</TamamEtk>
                            <Durum>20</Durum>
                        </SbOgrtDSUnitRDt>
                    </Units>
                </SODSRprDt>
            </SbDrsRprRslt>
        </SbDrsRprRspnse >
    </soap:Body>
</soap:Envelope>

我可以trace(returnXML)并查看上述所有内容。但是,当我尝试trace(returnXML.children().children().children())时,它什么也没有显示。此外,==null==undefined==" "==""返回false。我做错了什么?

修改

这是我在评论后所做的事情;

private function ProcessXML(ref:XML):void
{
    var returnXML:XML = new XML(ref);
    var soap:Namespace = new Namespace("http://www.w3.org/2003/05/soap-envelope");
    trace(returnXML.soap::Body.SbDrsRprRspnse.SbDrsRprRslt.SODSRprDt.Aktif); //it returns nothing!
}

编辑#2 我正在使用Flash Builder,它不断为第二个命名空间childrenNs - TypeError: Error 1080 - Illegal value for namespace抛出错误。我已经对错误进行了一些搜索,但是找不到值得的东西。所以也许使用一个namespace - soap来测试没有其他问题。那么这个XML只能使用一个命名空间吗?

1 个答案:

答案 0 :(得分:2)

这是因为xml中使用的命名空间。请检查我的代码并注意评论:

var testXml:XML = new XML('<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><SbDrsRprRspnse xmlns="http://tempuri.org/"><SbDrsRprRslt><SODSRprDt><Aktiv>false</Aktiv><Silindi>false</Silindi><Sira>0</Sira><Numara>IL1</Numara><AdSoyad>Maksim Tsygalha</AdSoyad><ToplamEtk>30</ToplamEtk><TamamEtk>6</TamamEtk><Durum>20</Durum><Units><SbOgrtDSUnitRDt><Aktif>false</Aktif><Silindi>false</Silindi><UnitsAdi>Ünite 3</UnitsAdi><EtkTop>30</EtkTop><TamamEtk>6</TamamEtk><Durum>20</Durum></SbOgrtDSUnitRDt></Units></SODSRprDt></SbDrsRprRslt></SbDrsRprRspnse ></soap:Body></soap:Envelope>');

// Here we take the main xml namespace
var soap:Namespace = testXml.namespace();
trace("The main xml: " +testXml.soap::Body);

// Here we take the main xml for the children
var childrenNs:Namespace = testXml.soap::Body.children()[0].namespace();

// When we have the main namespace and the children namespace,
// we may get information about children
trace("The first child: " + testXml.soap::Body.childrenNs::SbDrsRprRspnse);

// We may even set the children namespace as the default namespace
default xml namespace = childrenNs; 
// And after that we can work with children without the namespace
trace("The first child with default namespace: " + testXml.soap::Body.SbDrsRprRspnse);
trace("The Aktif child with default namespace: " + testXml.soap::Body..*.Aktif);

UPD 25-11-2015:

您也可以尝试删除有关命名空间的所有信息,并使用xml和常规xml一起使用:

var testXml:XML = new XML('<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><SbDrsRprRspnse xmlns="http://tempuri.org/"><SbDrsRprRslt><SODSRprDt><Aktiv>false</Aktiv><Silindi>false</Silindi><Sira>0</Sira><Numara>IL1</Numara><AdSoyad>Maksim Tsygalha</AdSoyad><ToplamEtk>30</ToplamEtk><TamamEtk>6</TamamEtk><Durum>20</Durum><Units><SbOgrtDSUnitRDt><Aktif>false</Aktif><Silindi>false</Silindi><UnitsAdi>Ünite 3</UnitsAdi><EtkTop>30</EtkTop><TamamEtk>6</TamamEtk><Durum>20</Durum></SbOgrtDSUnitRDt></Units></SODSRprDt></SbDrsRprRslt></SbDrsRprRspnse ></soap:Body></soap:Envelope>');

var testXmlString:String = testXml.toXMLString();
var regExp1:RegExp = /([<\/])(\w+\:)/g;
var regExp2:RegExp = /\sxmlns[^"]+"[^"]+"/g;
testXmlString = testXmlString.replace(regExp1, "$1");
testXmlString = testXmlString.replace(regExp2, "");
trace("testXmlString: " + testXmlString);

testXml = new XML(testXmlString);
trace("testXml.toXMLString(): " + testXml.toXMLString());