How to access xml nodes in flex

The web service returns this normal exception to my flex3 client:

<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>

      

This is obviously part of the FaultEvent object that I get when the remote call fails, so I am trying to access the values ​​of the subnode "T2gMsg" like this:

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())
 {
  //?
 }

      

Now I cannot figure out how to read the values ​​of ID, Severity, etc. I think that something like "x.ID" should work, but it doesn't, while x.child ("ID") or x.elements ("ID") return null. What can I do?

+2


source to share


3 answers


(as suggested, I go here to the solution I founded to close the question)

it is a question of namespaces: livedocs to explain that we need to qualify nodes to access them:



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);
}

      

+4


source


your xml is using namespaces, so you can try to access to someNode.name().localName

deep dive and use text () to get the value



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

      

0


source


thank. along with the online document and this discussion, I understand how to access the xml nodes of the namespace.
I think if the prefix is ​​not used as an example xml, you need to assign a namespace to the uri used.

<?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

      

So, while this is a bit longer, the "syntax" for accessing xml nodes is:

<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);    
}

      

0


source







All Articles