如何在flex中访问xml节点

时间:2009-09-02 11:03:06

标签: xml flex parsing

Web服务返回我的flex3客户端此自定义异常:

<SOAP-ENV:Fault xmlns:ro="urn:Gov2gLibrary" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:HNS="http://tempuri.org/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v1="http://tempuri.org/">
 <faultcode>E2gError</faultcode>
 <faultstring>abc</faultstring>
 <detail>
  <HNS:ROException>
   <HNS:Messages>
    <HNS:T2gMsg>
     <HNS:ID>4545</HNS:ID>
     <HNS:Severity>abc</HNS:Severity>
     <HNS:Category>abc</HNS:Category>
     <HNS:Message1>abc</HNS:Message1>
     <HNS:Message2 />
    </HNS:T2gMsg>
    <HNS:T2gMsg>
     <HNS:ID>345344</HNS:ID>
     <HNS:Severity>abc</HNS:Severity>
     <HNS:Category>abc</HNS:Category>
     <HNS:Message1>abc</HNS:Message1>
     <HNS:Message2 />
    </HNS:T2gMsg>
   </HNS:Messages>
  </HNS:ROException>
 </detail>
</SOAP-ENV:Fault>

这显然是我在远程调用失败时得到的FaultEvent对象的一部分,所以我试图访问“T2gMsg”子节点值,如下所示:

protected function onFaultEvent(e:FaultEvent):void
{
 var obj:Object = e.fault;
 var err:XMLList = obj.element.detail.children()[0].children();
 // now I have in err the "Messages" list, subnode of ROException,
 // so I should cycle to read one message at time:
 for each (var x:XML in err.children())
 {
  //?
 }

现在我无法弄清楚如何读取ID,Severity等值。我认为像“x.ID”这样的东西应该可以工作但不是,而x.child(“ID”)或x.elements(“ID”)返回null。我该怎么办?

3 个答案:

答案 0 :(得分:4)

(正如我所说,我正在向我提出解决问题的解决方案)

这是名称空间的问题:livedocs解释我们需要限定节点来访问它们:

var obj:Object = e.fault;
var doc:XML = obj.element.detail[0];
var err:XMLList = doc.children()[0].children(); // messages
var ns:Namespace = doc.namespace("HNS");
for each (var x:XML in err.children())
{
    trace(x.ns::ID);
    trace(x.ns::Severity);
    trace(x.ns::Category);
    trace(x.ns::Message1);
    trace(x.ns::Message2);
}

答案 1 :(得分:0)

你的xml使用命名空间,因此你可以尝试访问someNode.name().localName深入挖掘,并使用text()来获取值

for (var i:int = 0; i < x.length(); i++) {
    if (x[i].name().localName == "ID")  trace('x["ID"]: ' + x[i].text());
}

答案 2 :(得分:0)

感谢。连同在线文档和本讨论,我了解如何访问命名空间xml节点 我想,如果前缀未用作下面的示例xml,则需要为正在使用的uri分配名称空间。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <FindJunctionResponse xmlns="http://someserver/Service/NamePath">
      <FindJunctionResult>
        <OID>558</OID>
        <ClassID>5</ClassID>
        <Position>0</Position>
        <EID>0</EID>
        <XCoord>1662634.10015</XCoord>
        <YCoord>71634.435475</YCoord>
        <IsJunction>true</IsJunction>
        <IsFlag>false</IsFlag>
      </FindJunctionResult>
    </FindJunctionResponse>
  </soap:Body>
</soap:Envelope

所以即使它有点长,访问xml节点的“语法”也是:

<xml message>.<namespace class>::<xml node>.<name space class>

...

private function webServiceHandleResult(event:ResultEvent):void
{                            
    XML.ignoreWhitespace;
    var eXml:XML = new XML(event.message.body);
    var eXmlList:XMLList = eXml.children();
    var soapNS:Namespace = eXml.namespace("soap");
    var xmlnsNS:Namespace = new Namespace("http://someserver/Service/NamePath/")
    var resulteXmlList:XMLList = eXml.soapNS::Body.xmlnsNS::FindJunctionResponse.xmlnsNS::FindJunctionResult;
    for each (var myxml:XML in resulteXmlList.children())
    {
        //for each field, you can get the name and the value
        trace("field: " + myxml.localName() + ": " + myxml.valueOf());
    }
    //or reference each xml node by name.
    trace("OID: " = eXml.soapNS::Body.xmlnsNS::FindJunctionResponse.xmlnsNS::FindJunctionResult.xmlnsNS::OID);
    trace("ClassID: " = eXml.soapNS::Body.xmlnsNS::FindJunctionResponse.xmlnsNS::FindJunctionResult.xmlnsNS::ClassID);
    trace("Position: " = eXml.soapNS::Body.xmlnsNS::FindJunctionResponse.xmlnsNS::FindJunctionResult.xmlnsNS::Position);
    trace("EID: " = eXml.soapNS::Body.xmlnsNS::FindJunctionResponse.xmlnsNS::FindJunctionResult.xmlnsNS::EID);
    trace("XCoord: " = eXml.soapNS::Body.xmlnsNS::FindJunctionResponse.xmlnsNS::FindJunctionResult.xmlnsNS::XCoord);    
    trace("YCoord: " = eXml.soapNS::Body.xmlnsNS::FindJunctionResponse.xmlnsNS::FindJunctionResult.xmlnsNS::YCoord);    
    trace("IsJunction: " = eXml.soapNS::Body.xmlnsNS::FindJunctionResponse.xmlnsNS::FindJunctionResult.xmlnsNS::IsJunction);
    trace("IsFlag: " = eXml.soapNS::Body.xmlnsNS::FindJunctionResponse.xmlnsNS::FindJunctionResult.xmlnsNS::IsFlag);    
}