2011-04-08 59 views
3

我需要從我的數據庫中獲取數據來創建單獨的xml文件。我已經完成了大部分工作,但我不知道如何從XML字段獲取數據並將其轉移到我創建的XMLDocument中。當我嘗試以文本的形式進行操作時,它無法正常工作。如何從SQL Server 2005中獲取XML數據並將其作爲節點插入到TXMLDocument中?

這裏是我到目前爲止的代碼:

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">?&lt;component xmlns="urn:hl7-org:v3"&gt;&lt;section ID="Section_5"&gt;&lt;id root="2e0bdeb7-2254-4217-b6b6-523848d65112"/&gt &lt/section&gt;&lt;/component&gt;</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)我如何從xmlfield獲取XML而不是XML文檔,以及2)如何在「警告和注意事項」節點下插入一段XML作爲子項?

感謝任何人都可以幫助我!

特里

回答

3

如果我正確理解你的問題,你需要從現場讀取XML字符串,並插入到一個XML節點。你可以這樣做,創建一個虛擬 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> 
相關問題