string to xmlNode delphi (or how to add an xml fra

2019-06-24 06:27发布

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!

2条回答
Summer. ? 凉城
2楼-- · 2019-06-24 07:25

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);
查看更多
ゆ 、 Hurt°
3楼-- · 2019-06-24 07:34

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.

查看更多
登录 后发表回答