在Javascript中将XMLDocument对象转换为String

时间:2021-01-15 22:25:53

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

我想将我从ajax请求获得的XMLDocument对象转换为字符串。我试过用

new XMLSerializer()).serializeToString(xmlObject)

and I get the following response:-

我收到以下回复: -

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

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

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

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

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.

我不想要这部分回复。有没有办法做到这一点。注意:我不想使用“substr”等变通方法。

1 个解决方案

#1


40  

You can do this by serializing just the root node:

您可以通过仅序列化根节点来执行此操作:

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

Demo: http://jsfiddle.net/timdown/LmWkL/

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

#1


40  

You can do this by serializing just the root node:

您可以通过仅序列化根节点来执行此操作:

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

Demo: http://jsfiddle.net/timdown/LmWkL/

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