我需要通过Inno Setup将CDATA值保存到XML文件中。
我搜索了Msxml2.DOMDocument.6.0
文档,但没有成功地将值正确写入节点的方法。
如果我只想在代码中用我想要的值XMLNode.Text := AValue;
声明ExpandConstant('<string><![CDATA[my value]]></string>);
,则XML解释器将所有字符'<>'
替换为XML实体<
,然后>
。
function SaveValueToXML(const AFileName, APath, AValue : string): string;
var
XMLNode: Variant;
XMLDocument: Variant;
begin
Result := '';
XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0');
try
XMLDocument.async := False;
XMLDocument.load(AFileName);
if (XMLDocument.parseError.errorCode <> 0) then
MsgBox('The XML file could not be parsed. ' +
XMLDocument.parseError.reason, mbError, MB_OK)
else
begin
XMLDocument.setProperty('SelectionLanguage', 'XPath');
XMLDocument.setProperty(
'SelectionNamespaces', 'xmlns:ns=''urn:mathworks.matlab.settings''');
XMLNode := XMLDocument.selectSingleNode(APath);
XMLNode.Text := AValue;
XMLDocument.save(AFileName);
end;
except
MsgBox('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK);
end;
end;
function NextButtonClick(PageID: Integer): Boolean;
var
XMLFile: string;
begin
Result := True;
if (PageId = wpReady) then
begin
XMLFile := ExpandConstant('path to xml file');
if FileExists(XMLFile) then
begin
SaveValueToXML(
XMLFile, '//ns:key[@name=''InstallationFolder'']',
ExpandConstant('<![CDATA[value to write]]></string>'));
end;
end;
end;
是否可以使用Inno Setup用Msxml2.DOMDocument.6.0
声明CDATA节?我尝试使用转义字符,并且使用XMLNode := XMLNode.createCDATASection(Avalue);
语法也产生了相同的结果,但没有成功...
XML文件包含:
<?xml version="1.0" encoding="UTF-8"?>
<settings name="matlab" visible="true" xmlns="urn:mathworks.matlab.settings" xsi:schemaLocation="urn:mathworks.matlab.settings settings.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- ... -->
<settings name="addons">
<!-- ... -->
<key name="InstallationFolder">
<string></string>
</key>
</settings>
</settings>
我们需要将XML修改为以下代码:
<?xml version="1.0" encoding="UTF-8"?>
<settings name="matlab" visible="true" xmlns="urn:mathworks.matlab.settings" xsi:schemaLocation="urn:mathworks.matlab.settings settings.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- ... -->
<settings name="addons">
<!-- ... -->
<key name="InstallationFolder">
<string>
<value><![CDATA[my value]]></value>
</string>
</key>
</settings>
</settings>
最终代码:
const
NODE_ELEMENT = 1;
(*Function to load and save value to an XML file*)
function SaveValueToXML(const AFileName, APath, AValue : string): string;
var
XMLNode: Variant;
XMLNode2: Variant;
XMLDocument: Variant;
begin
Result := '';
XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0');
try
XMLDocument.async := False;
XMLDocument.load(AFileName);
if (XMLDocument.parseError.errorCode <> 0) then
MsgBox('The XML file could not be parsed. ' +
XMLDocument.parseError.reason, mbError, MB_OK)
else
begin
XMLDocument.setProperty('SelectionLanguage', 'XPath');
XMLDocument.setProperty('SelectionNamespaces', 'xmlns:ns=''urn:mathworks.matlab.settings''');
XMLNode := XMLDocument.selectSingleNode(APath);
XMLNode2 := XMLDocument.createNode(NODE_ELEMENT, 'value', 'urn:mathworks.matlab.settings');
XMLNode2.appendChild(XMLDocument.createCDATASection(AValue));
XMLNode.appendChild(XMLNode2);
XMLDocument.save(AFileName);
end;
except
MsgBox('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK);
end;
end;
function NextButtonClick(PageID: Integer): Boolean;
var
XMLFile: string;
begin
Result := True;
if (PageId = wpReady) then
begin
XMLFile := ExpandConstant('{userappdata}\MathWorks\MATLAB\R2018b\matlab.settings');
if FileExists(XMLFile) then
begin
SaveValueToXML(XMLFile, '//ns:key[@name=''InstallationFolder'']/ns:string', ExpandConstant('{userappdata}\MathWorks\MATLAB Add-Ons'));
end;
end;
end;
答案 0 :(得分:0)
createCDATASection
是“文档”而不是“节点”的方法。
这对我有用:
func parseFile(fileName:String, fileType:String)->String! {
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
let filepath = getDocumentsDirectory().appendingPathComponent(fileName + fileType)
var fileString = ""
fileString = filepath.absoluteString
do {
var contents = try String(contentsOfFile: fileString, encoding: .utf8)
return contents
} catch {
print("File Read Error for file \(filepath)")
print("Error info: \(error)")
return nil
}
}
整个代码,包括创建XMLNode.appendChild(XMLDocument.createCDATASection(AValue));
节点:
value
const
NODE_ELEMENT = 1;
此外,您的XPath还需要选择内部XMLNode2 := XMLDocument.createNode(NODE_ELEMENT, 'value', 'urn:mathworks.matlab.settings');
XMLNode2.appendChild(XMLDocument.createCDATASection('my value'));
XMLNode.appendChild(XMLNode2);
节点:
string
结果:
//ns:key[@name='InstallationFolder']/ns:string