我需要从数据库中获取数据以创建单独的xml文件。我已经完成了大部分工作,但我不知道如何从XML字段和我正在创建的XMLDocument中获取数据。当我尝试将其作为文本进行操作时,它无法正常工作。
到目前为止,这是我的代码:
XMLDoc.Active := true;
aNode := XMLDoc.AddChild('item');
aNode.SetAttribute('id','Drug');
bNode := aNode.AddChild('item');
bNode.SetAttribute('id','LDPId');
bNode.Text := IntToStr(vwFirstLifeLabelId.Value); // This works fine.
bNode := aNode.AddChild('item');
bNode.SetAttribute('id','Indications and Usage');
bNode.Text := vwFirstLifeIndicationsandUsage.AsString; // This doesn't work!
我在xml中得到的是:
<item id="Drug">
<item id="LDPId">38696</item>
<item id="Warnings and Precautions">?<component xmlns="urn:hl7-org:v3"><section ID="Section_5"><id root="2e0bdeb7-2254-4217-b6b6-523848d65112"/> </section></component></item>
</item>
代替:
<item id="Drug">
<item id="LDPId">38696</item>
<item id="Warnings and Precautions">
<component xmlns="urn:hl7-org:v3">
<section ID="Section_5">
<id root="2e0bdeb7-2254-4217-b6b6-523848d65112"/>
</section>
</component>
</item>
</item>
我想我有两个问题:1)如何将XML作为XML而不是文本从xmlfield中获取,以及2)如何在“警告和注意事项”节点下插入一段XML作为子项?
感谢任何人都能给我提供的任何帮助!
特里
答案 0 :(得分:3)
如果我理解你的问题,你需要从字段中读取XML字符串并插入到Xml节点中。您可以创建 Dummy Xml文档并加载Xml字符串,然后检索DocumentElement
属性并分配给您想要的节点。
检查此示例
const
XmlStr =
'<component xmlns="urn:hl7-org:v3">'+
'<section ID="Section_5">'+
'<id root="2e0bdeb7-2254-4217-b6b6-523848d65112"/>'+
'</section>'+
'</component>';
var
oXmlDoc : IXMLDocument;
cXmlDoc : IXMLDocument;
Node : IXMLNode;
begin
oXmlDoc := TXMLDocument.Create(nil);
try
oXmlDoc.Options := [doNodeAutoIndent];
oXmlDoc.Active := true;
Node:=oXmlDoc.AddChild('item');
cXmlDoc := TXMLDocument.Create(nil);
try
cXmlDoc.Active := true;
cXmlDoc.LoadFromXML(XmlStr);
//assing the XML to the Node
Node.ChildNodes.Add(cXmlDoc.DocumentElement);
finally
cXmlDoc:=nil;
end;
finally
oXmlDoc:=nil;
end;
end;
结果将是
<item>
<component xmlns="urn:hl7-org:v3">
<section ID="Section_5">
<id root="2e0bdeb7-2254-4217-b6b6-523848d65112"/>
</section>
</component>
</item>