简体   繁体   English

如何在Flex中访问xml节点

[英]how to access xml nodes in flex

A web service return to my flex3 client this custom exception: 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>

This is obviously a part of the FaultEvent object I get when the remote call fail, so I'm trying to access "T2gMsg" subnode values like this: 当远程调用失败时,这显然是我获得的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())
 {
  //?
 }

Now I can't figure out how to read ID, Severity etc values. 现在我不知道如何读取ID,严重性等值。 I think something like "x.ID" should work but it's not, while x.child("ID") or x.elements("ID") return null. 我认为类似“ x.ID”的东西应该可以,但不能,而x.child(“ ID”)或x.elements(“ ID”)返回null。 What can I do? 我能做什么?

(as suggested I'm moving here the solution I founded to close the question) (如建议,我将在此创建为解决问题而创建的解决方案)

it was a matter of namespaces: livedocs explain we need to qualify nodes to access them: 这是一个名称空间的问题: 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);
}

your xml uses namespaces, so you can try to access someNode.name().localName to dig deep inside, and use text() to get the value 您的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());
}

thanks. 谢谢。 along with the online doc and this discussion I understand how to access the namespace xml nodes. 连同在线文档和此讨论,我了解了如何访问名称空间xml节点。
I guess, if a prefix is not used as the below example xml, you need to assign the namespace for the uri being used. 我想,如果未将前缀用作以下示例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

So even though it's a little long, the "syntax" to access the xml nodes would be: 因此,即使它有点长,访问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);    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM