请教将xml中的数据插入表中的最佳方法(50分)

  • 主题发起人 主题发起人 fql
  • 开始时间 开始时间
F

fql

Unregistered / Unconfirmed
GUEST, unregistred user!
declare @idoc int
declare @doc varchar(1000)
set @doc ='
<ROOT>
<Customers CustomerID="VINET" ContactName="Paul Henriot">
<Orders CustomerID="VINET" EmployeeID="5" OrderDate=
"1996-07-04T00:00:00">
<Order_x0020_Details OrderID="10248" ProductID="11" Quantity="12"/>
<Order_x0020_Details OrderID="10248" ProductID="42" Quantity="10"/>
</Orders>
</Customers>
<Customers CustomerID="LILAS" ContactName="Carlos Gonzlez">
<Orders CustomerID="LILAS" EmployeeID="3" OrderDate=
"1996-08-16T00:00:00">
<Order_x0020_Details OrderID="10283" ProductID="72" Quantity="3"/>
</Orders>
</Customers>
</ROOT>'
exec sp_xml_preparedocument @idoc OUTPUT, @doc
insert tablename
SELECT *
FROM OPENXML (@idoc, '/ROOT/Customers') with tablename
EXEC sp_xml_removedocument @idoc
以上是sql server的例子,是将xml文档中的数据插入sql server的表中,我要问的是
能不能把
set @doc ='
<ROOT>
<Customers CustomerID="VINET" ContactName="Paul Henriot">
<Orders CustomerID="VINET" EmployeeID="5" OrderDate=
"1996-07-04T00:00:00">
<Order_x0020_Details OrderID="10248" ProductID="11" Quantity="12"/>
<Order_x0020_Details OrderID="10248" ProductID="42" Quantity="10"/>
</Orders>
</Customers>
<Customers CustomerID="LILAS" ContactName="Carlos Gonzlez">
<Orders CustomerID="LILAS" EmployeeID="3" OrderDate=
"1996-08-16T00:00:00">
<Order_x0020_Details OrderID="10283" ProductID="72" Quantity="3"/>
</Orders>
</Customers>
</ROOT>'
换成一个xml文件名,等于说就是传入一个文件名要将文件中的内容插入表中,不要传入文件的内容
大家有没有什么更好的方法将xml中的数据插入表中呢
 
我也在找,帮你提前
 
请参考以下代码,相应改变即可实现。抱歉没有替你[:(]
procedure DataSetToDOM(RootName, RecordName: string;
XMLDoc: TXMLDocument;
DataSet: TDataSet);
var
iNode, iChild: IXMLNode;
i: Integer;
begin
DataSet.Open;
DataSet.First;
// root
XMLDoc.DocumentElement := XMLDoc.CreateNode (RootName);

// add table data
while not DataSet.EOFdo
begin
// add a node for each record
iNode := XMLDoc.DocumentElement.AddChild (RecordName);
for I := 0 to DataSet.FieldCount - 1do
begin
// add an element for each field
iChild := iNode.AddChild (DataSet.Fields.FieldName);
iChild.Text := DataSet.Fields.AsString;
end;
DataSet.Next;
end;
DataSet.Close;
end;

proceduredo
mToTree(XmlNode: IXMLNode;
TreeNode: TTreeNode);
var
I: Integer;
NewTreeNode: TTreeNode;
NodeText: string;
AttrNode: IXMLNode;
begin
// skip text nodes and other special cases
if not (XmlNode.NodeType = ntElement) then
Exit;
// add the node itself
NodeText := XmlNode.NodeName;
if XmlNode.IsTextElement then
NodeText := NodeText + ' = ' + XmlNode.Text;
NewTreeNode := TreeView1.Items.AddChild(TreeNode, NodeText);
// add attributes
for I := 0 to xmlNode.AttributeNodes.Count - 1do
begin
AttrNode := xmlNode.AttributeNodes.Nodes;
TreeView1.Items.AddChild(NewTreeNode,
'[' + AttrNode.NodeName + ' = "' + AttrNode.Text + '"]');
end;
// add each child node
if XmlNode.HasChildNodes then
for I := 0 to xmlNode.ChildNodes.Count - 1do
do
mToTree (xmlNode.ChildNodes.Nodes , NewTreeNode);
end;
 
后退
顶部