在 AS3 中使用 E4X 读取非标准命名空间 XML?
我正在尝试解析 AS3 中的一些 XML,我通过对 C# 的 WebService 调用收到这些 XML。 C# 使用 DataContract 进行序列化,因此命名空间不是标准的。
xml 如下所示:
<User xmlns="http://schemas.datacontract.org/2004/07/UserDatabaseManipulation.POCO" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Email>
<EmailString>
[email protected]
</EmailString>
</Email>
<Password>
<PasswordPlainText>
password
</PasswordPlainText>
</Password>
<ReferralDetails>
<ReferralEmail/>
<ServiceCreatedAt>
google
</ServiceCreatedAt>
</ReferralDetails>
<UserDetails>
<Address>
Penn Ave
</Address>
<City>
Washington DC
</City>
<Country>
USA
</Country>
<FirstName>
Bill
</FirstName>
<LastName>
Clinton
</LastName>
<State>
AK
</State>
<Zip>
11111
</Zip>
</UserDetails>
</User>
正如您所看到的,我有一个由电子邮件、密码、推荐详细信息和用户详细信息组成的用户。
这是我解析它和问题的地方:
private function onResult(event:ResultEvent):void
{
var n:Namespace = new Namespace("http://schemas.datacontract.org/2004/07/UserDatabaseManipulation.POCO");
use namespace n;
//This WORKS! ResultXml is loaded with the correct looking XML.
var resultXml:XML = new XML(event.result);
//This doesnt work! I just end up with an empty XMLList.
var email:Object = resultXml.Email;
...
这是调试视图中的屏幕截图(复制链接并重新查看以查看更大的内容):
没有 e4x,我可以让它像这样工作,但它真的很笨重:
var resultXml:XML = new XML(event.result); // the whole block of XML
var email:XML = resultXml.children()[0]; // the email object XML
var emailText:XML = email.children()[0]; // the email text
var emailActualXml:XML = emailText.children()[0]; // the email string in xml
var emailString:String = emailActualXml.toString();
屏幕截图:
解决方案如下
var xmlNamespace:Namespace = new Namespace( // namespace in here );
var resultXml:XML = new XML(event.result);
var email:XMLList = resultXml.xmlNamespace::Email;
var emailString:Object = email.xmlNamespace::EmailString.text().toString();
I am trying to parse some XML in AS3 that I recieve thru a WebService call to C#. C# is serializing using a DataContract so the namespace is non standard.
Here is what the xml looks like:
<User xmlns="http://schemas.datacontract.org/2004/07/UserDatabaseManipulation.POCO" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Email>
<EmailString>
[email protected]
</EmailString>
</Email>
<Password>
<PasswordPlainText>
password
</PasswordPlainText>
</Password>
<ReferralDetails>
<ReferralEmail/>
<ServiceCreatedAt>
google
</ServiceCreatedAt>
</ReferralDetails>
<UserDetails>
<Address>
Penn Ave
</Address>
<City>
Washington DC
</City>
<Country>
USA
</Country>
<FirstName>
Bill
</FirstName>
<LastName>
Clinton
</LastName>
<State>
AK
</State>
<Zip>
11111
</Zip>
</UserDetails>
</User>
So as you can see from that I have a User which consists of Email, Password, Referral Details, and UserDetails.
Here is where I parse it and the problem:
private function onResult(event:ResultEvent):void
{
var n:Namespace = new Namespace("http://schemas.datacontract.org/2004/07/UserDatabaseManipulation.POCO");
use namespace n;
//This WORKS! ResultXml is loaded with the correct looking XML.
var resultXml:XML = new XML(event.result);
//This doesnt work! I just end up with an empty XMLList.
var email:Object = resultXml.Email;
...
Here's a screen shot in debug view (copy link and re-view to see it bigger):
Without e4x I can get it to work like this but it is really clunky:
var resultXml:XML = new XML(event.result); // the whole block of XML
var email:XML = resultXml.children()[0]; // the email object XML
var emailText:XML = email.children()[0]; // the email text
var emailActualXml:XML = emailText.children()[0]; // the email string in xml
var emailString:String = emailActualXml.toString();
Screenshot:
HERES THE SOLUTION
var xmlNamespace:Namespace = new Namespace( // namespace in here );
var resultXml:XML = new XML(event.result);
var email:XMLList = resultXml.xmlNamespace::Email;
var emailString:Object = email.xmlNamespace::EmailString.text().toString();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当涉及命名空间时,必须使用完全限定名称(包括命名空间)。
或者使用 默认 xml 命名空间 指令
You must use the fully qualified name (including the namespace) when there are namespaces involved.
Or use the default xml namespace directive
试试这个:
当然,您可以使用点语法直接访问 EmailString!
Try this:
Of course you could access EmailString directly with the dot syntax!