在Javascript中将XMLDocument对象转换为String [英] Converting XMLDocument object to String in Javascript

查看:38
本文介绍了在Javascript中将XMLDocument对象转换为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将作为响应从ajax请求获得的XMLDocument对象转换为字符串.我尝试使用

I want to convert XMLDocument object that I'm getting as a response from an ajax request, to a string. I tried using

new XMLSerializer()).serializeToString(xmlObject)

,我得到以下答复:-

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:errorList xmlns:ns2="http://www.example.com/api/delivery/V1"><error code="DOMAIN_VALIDATE" path="delivery.shipper"><message>empty</message></error><error code="DOMAIN_VALIDATE" path="delivery.shipperSite"><message>empty</message></error><error code="DOMAIN_VALIDATE" path="delivery.leg"><message>invalid</message></error></ns2:errorList>

意味着该方法将整个XMLDocument转换为字符串,包括第一个标签

Means the method converted the whole XMLDocument into string, including the very first tag

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

我不希望这部分回应.有什么方法可以做到这一点.注意:我不想使用"substr"等替代方法.

I don't want this part of the response. Is there any method that does that. Note: I don't want to use the workarounds like "substr" etc.

推荐答案

您可以通过仅对根节点进行序列化来实现:

You can do this by serializing just the root node:

new XMLSerializer().serializeToString(xmlObject.documentElement);

演示: http://jsfiddle.net/timdown/LmWkL/

这篇关于在Javascript中将XMLDocument对象转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆