我试图在以下方式构建的xml中强制执行编码:
XmlWriterSettings xmlSetting = new XmlWriterSettings();
XmlWriter xmlWriter;
Str res;
xmlSetting.encoding ('UTF-8');
xmlWriter = XmlWriter::newXml(xmlSetting);
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement('root');
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
xmlWriter.flush();
res = xmlWriter.writeToString();
info(res);
但是我的系统中的结果是:
<?xml version="1.0" encoding="utf-16"?><root />
什么是强制编码为UTF-8的正确方法,是否可行
如果您正在写一个字符串,那么编码将被UTF-16覆盖。字符串始终是Unicode,即UTF-16。使用writeToString
方法无法编写任何其他编码。如果您写入文件,那么您的编码将生效。
XmlWriterSettings xmlSetting = new XmlWriterSettings();
XmlWriter xmlWriter;
Str res;
xmlSetting.encoding ('UTF-8');
xmlWriter = XMLWriter::newFile(@"C:\TEMP\test.xml", xmlSetting);
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement('root');
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
xmlWriter.flush();