简体   繁体   English

如何在Mozilla Rhino(Javascript 1.6)中将XML序列化为字符串?

[英]How can I serialize XML to a string in Mozilla Rhino (Javascript 1.6)?

I need to serialize a Javascript XML DOM object to an XML string. 我需要将Javascript XML DOM对象序列化为XML字符串。

Unfortunately, the only option for doing so in the version of Rhino I'm stuck with (implements Javascript 1.6) appears to be E4X and its toXMLString() method. 不幸的是,在我所坚持的Rhino版本(实现Javascript 1.6)中,这样做的唯一选择似乎是E4X及其toXMLString()方法。 This method does produce an XML string, but one formatted for human reading, with whitespace where it shouldn't have any. 此方法的确会产生一个XML字符串,但会格式化为供人类阅读的XML字符串,并在其中不应包含空格。

Example - The output I want: 示例-我想要的输出:

<Raster id="2" guid="12345" comment="">54321<Matrix>7|0|0|7|651|869</Matrix><MetaData/></Raster> 

The output I get: 我得到的输出:

<Raster id="2" guid="12345" comment=""> 
    54321 
    <Matrix>7|0|0|7|651|869</Matrix> 
    <MetaData/> 
</Raster> 

The whitespace before "54321" is significant and causes problems later when the output XML gets parsed. “ 54321”之前的空格很重要,并且在以后解析输出XML时会引起问题。

I've been looking for an alternative XML serialization, but nothing seems to be available for this older Rhino version. 我一直在寻找替代的XML序列化,但是对于此较旧的Rhino版本似乎没有任何可用。

XMLSerializer would be ideal but it's not available in Rhino 1.6. XMLSerializer会是理想的选择,但Rhino 1.6中不提供。

The XPCOM XMLSerializer from Mozilla's xmlextras would do the job as well, but the XPCOM Components object doesn't exist in Rhino 1.6. 来自Mozilla xmlextras的XPCOM XMLSerializer也可以完成此工作,但是Rhino 1.6中不存在XPCOM Components对象。

Does anyone have any ideas where I could get an XML serializer that properly preserves whitespace and works with Alfresco's Rhino (Javascript 1.6)? 有谁知道我可以得到一个XML序列化程序,该序列化程序可以正确保留空白并可以与Alfresco的Rhino(Javascript 1.6)协同工作?

XML.prettyPrinting = false;

应该做你想做的

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

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