问题:它会改变原始XML文件。
<?xml version="1.0" encoding="utf-8"?>
转化为
<?xml version="1.0" encoding="utf-16"?>
以下是我从Exchange服务器收到的XML:
这是我从Exchange服务器收到的XML:
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="14" MinorVersion="0" MajorBuildNumber="722" MinorBuildNumber="0" Version="Exchange2010" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<m:ResolveNamesResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<m:ResponseMessages>
<m:ResolveNamesResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
<m:ResolutionSet TotalItemsInView="1" IncludesLastItemInRange="true">
<t:Resolution>
<t:Mailbox>
<t:Name>developer</t:Name>
<t:EmailAddress>developer@timetellbv.nl</t:EmailAddress>
<t:RoutingType>SMTP</t:RoutingType>
<t:MailboxType>Mailbox</t:MailboxType>
</t:Mailbox>
<t:Contact>
<t:Culture>nl-NL</t:Culture>
<t:DisplayName>developer</t:DisplayName>
<t:GivenName>developer</t:GivenName>
<t:EmailAddresses>
<t:Entry Key="EmailAddress1">SMTP:developer@timetellbv.nl</t:Entry>
</t:EmailAddresses>
<t:ContactSource>ActiveDirectory</t:ContactSource>
</t:Contact>
</t:Resolution>
</m:ResolutionSet>
</m:ResolveNamesResponseMessage>
</m:ResponseMessages>
</m:ResolveNamesResponse>
</s:Body>
</s:Envelope>
这是一个删除命名空间信息的函数:
Uses
MSXML2_TLB; // IXMLDOMdocument
class function TXMLHelper.RemoveNameSpaces(XMLString: String): String;
const
// An XSLT script for removing the namespaces from any document.
// From http://wiki.tei-c.org/index.php/Remove-Namespaces.xsl
cRemoveNSTransform =
'<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">' +
'<xsl:output method="xml" indent="no"/>' +
'<xsl:template match="/|comment()|processing-instruction()">' +
' <xsl:copy>' +
' <xsl:apply-templates/>' +
' </xsl:copy>' +
'</xsl:template>' +
'<xsl:template match="*">' +
' <xsl:element name="{local-name()}">' +
' <xsl:apply-templates select="@*|node()"/>' +
' </xsl:element>' +
'</xsl:template>' +
'<xsl:template match="@*">' +
' <xsl:attribute name="{local-name()}">' +
' <xsl:value-of select="."/>' +
' </xsl:attribute>' +
'</xsl:template>' +
'</xsl:stylesheet>';
var
Doc, XSL: IXMLDOMdocument2;
begin
Doc := ComsDOMDocument.Create;
Doc.ASync := false;
XSL := ComsDOMDocument.Create;
XSL.ASync := false;
try
Doc.loadXML(XMLString);
XSL.loadXML(cRemoveNSTransform);
Result := Doc.TransFormNode(XSL);
except
on E:Exception do Result := E.Message;
end;
end; { RemoveNameSpaces }
但在此之后,它突然变成了一个utf-16文档:
<?xml version="1.0" encoding="UTF-16"?>
<Envelope>
[snip]
</Envelope>
在谷歌搜索“xsl utf-8 utf-16”后,我尝试了几件事:
Change the line (e.g. Output DataTable XML in UTF8 rather than UTF16)
<xsl:output method="xml" indent="no">
into either:
<xsl:output method="xml" encoding="utf-8" indent="no"/> <xsl:output method="xml" encoding="utf-8"/> <xsl:output encoding="utf-8"/>
That did not work.
(It would be the optimal solution, according to http://www.xml.com/pub/a/2002/09/04/xslt.html "The encoding attribute actually does more than add an encoding declaration to the result document; it tells the XSLT processor to write out the result using that encoding.")Change the line (e.g. XslCompiledTransform uses UTF-16 encoding)
<xsl:output method="xml" indent="no"/>
into
<xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
which leaves out the starting xml tag, but if I then just prepend
<?xml version="1.0" encoding="utf-8"?>
I will lose characters because no actual utf conversion is done.
IXMLDOMdocument2 does not have an
Encoding
property
有什么想法可以解决这个问题吗?
备注/背景:
如果所有其他方法都失败了,也许还有将 utf-16 XML 数据更改为 utf-8 的选项,但那是完全不同的方法。
我正在尝试做所有事情都使用 utf-8,因为我通过 EWS 与 Exchange 服务器通信,将 http 请求头设置为 utf-16 不起作用:Exchange 告诉我内容类型“text/xml; charset=utf-16”不是预期类型“text/xml; charset=utf-8”。EWS 返回 utf-8(请参见帖子开头)。
iXMLDocument.SaveToXML(AUTF8String)
。 - pf1957SaveToXML
的方法。它在 DOM 文档上拥有一个名为save
的方法,我的建议是在创建了一个空的 DOM 文档后传递给transformNodeToObject
方法并使用该方法。这样,如果您保存到文件或流中,则编码应该符合预期。如果您使用transformNode
,则这是不可能的。 - Martin HonnenIXMLDocument
/IXMLNode
来调用。有重载的方法TransformNode
,其中一个调用了transformNodeToObject
。我的评论涉及到需要执行某种保存操作以确保正确的编码。可以通过调用SaveToXML并传递类型为UTF8String的var参数来轻松保存它。 - pf1957Doc.XML.Text
或XSL.XML.Text
想要实现什么。使用loadXML(string)
将带有XML / XSLT标记的字符串解析为DOM文档。您到底想要实现什么,您正在寻找什么样的结果?如果您需要一个具有转换结果的文件,我强烈建议您调用Res.Save("result.xml")
让MSXML创建该文件。如果您不想要具有转换结果的文件,而只是想要一个字符串,则我不确定尝试使用transformNodeToObject
是否有所帮助,DOM文档的xml
属性也是UTF-16编码的字符串。 - Martin Honnen