如何使用 e4x 生成的 XML 以 HTML 方式关闭元素?
我正在使用 e4x 为我的用户生成一个 HTML 片段,他们可以复制并查看该片段。粘贴到他们的博客或网站中:
var xml: XML = <object>
<param name="..."></param>
<param name="..."></param>
<embed args="..."></embed>
</object>;
var html: String = xml.toXMLString();
但是,没有子元素的标签使用较短的 变体而不是
有没有办法强制以 HTML 方式关闭标签,或者有一个好的解决方法吗?在 和
之间添加注释似乎不起作用。
I'm using e4x to generate a HTML-snippet for my users which they can copy & paste into their blogs or web sites:
var xml: XML = <object>
<param name="..."></param>
<param name="..."></param>
<embed args="..."></embed>
</object>;
var html: String = xml.toXMLString();
however, the tags without child elements are closed using the shorter <embed/>
-variant rather than <embed></embed>
in the output HTML-string , which causes some problems when the code is pasted into e.g. the Blogger editor.
Is there a way to force the HTML-way of closing tags or is there a good workaround? Putting comments between <embed>
and </embed>
doesn't seem to work..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 XML 对象中表示 HTML 在这里似乎不是一个好主意。 HTML 片段无论如何都只会被视为文本:您不会对其执行任何 XML 操作。
只需将其直接构建为字符串即可。这样,它的格式就完全按照您的需要,您可以包含注释,...
如果您确实愿意,您可以通过确保该 xml 元素的内容不为空,使其输出带有 XML 对象的单独结束标记。您不希望文本之间有任何实际文本,但只需一个空格就足够了。在 Flex 中(我不知道其他动作脚本实现),您可能必须将 XML.ignoreWhitespace 设置为 false 才能正常工作。
Representing HTML in XML objects doesn't seem like a good idea here. The HTML-snippet will only be treated as text anyways: you won't do any XML operations on it.
Just build it directly as a String. That way it is formatted exactly as you want, you can include comments,...
If you really want to, you can make it output a separate closing tag with the XML object by making sure the content of that xml element is not empty. You don't want any real text between the text, but just a space would be enough. In Flex (I don't know about other actionscript implementations) you'll probably have to set XML.ignoreWhitespace to false for that to work.