string to xmlNode delphi (or how to add an xml fragment to TXMLDocument)

时间秒杀一切 提交于 2019-12-10 15:24:28

问题


I Have a few text strings that contain well formed XML.

I would like to be able to (1) turn these strings into IXMLNodes then (2) append them to an existing XMLDocument. Preferably without declaring a new XMLDocument first.

This doesn't seem possible?

Is there any easy way to accomplish something equivalent though? My initial thought was to use the IXMLNode.XML (string) property and insert the new strings. No such luck as IXMLNode.XML is Read Only.

Here is an example, if I had the following strings in a TStringList,

<Property Name="Version" RttiType="tkString"></Property>
<Property Name="ShowSubunit" RttiType="tkBoolean"></Property>

And I had the following XML, already loaded into a TXMLDocument, how could I easily append the two lines above into the TXMLDocument below?

<Program Name="PFOO">
  <Class Name="CFOO">
    <Property Name="DBN" RttiType="tkString"/>
    <Property Name="SDate" RttiType="tkClass" ClassType="TXSDATE">12/30/1899</Property>
    <Property Name="XForm" RttiType="tkEnumeration">xfXML</Property>
    <Property Name="Singleton" RttiType="tkBoolean">True</Property>
  </Class>
</Program>

Any other (simple) ways to achieve this (no protected hack on the XML property please)?

Thank you!


回答1:


Unless you parse the XML fragments manually and then construct the relevant child nodes/attributes manually, you will have to load the fragments into a temp XMLDocument and then move its nodes to the main XMLDocument as needed.

Update: For example:

Node := XmlDocument1.DocumentElement.ChildNodes[0]; // <Class> node
Node.ChildNodes.Add(LoadXMLData('<Property Name="Version" RttiType="tkString"></Property>').DocumentElement);
Node.ChildNodes.Add(LoadXMLData('<Property Name="ShowSubunit" RttiType="tkBoolean"></Property>').DocumentElement);



回答2:


Check out SimpleStorage. For now its tied to OmniXML, but its powerfull. What you want would look like this:

CurrentNode.Append(StorageFromXML('<Node>Content</Node>'));

One line of code.



来源:https://stackoverflow.com/questions/16743380/string-to-xmlnode-delphi-or-how-to-add-an-xml-fragment-to-txmldocument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!