如何从XML中提取子节点值作为字符串(How to extract child node value from XML as string)

我试图循环一些XML并将字符串的值设置为等于特定节点内容。 XML看起来像:

<RootNode> <SubNode>test<SubNode> <SubNode><ExtraMarkup>some value</ExtraMarkup><SubNode> </RootNode>

每个子节点可以包含一个值或其他XML子节点。 对于第一个子节点,此代码正常工作:

for Node := 0 to RootNode.childNodes.length-1 do begin AttrValue := RootNode.childNodes[Node].selectSingleNode('SubNode').Text; // More code here... end;

问题是当子节点包含子节点时。 我希望AttrValue的值为' test '或' <ExtraMarkup>some value</ExtraMarkup> '作为字符串。

如果不是文本而是获取XML属性,则不会保留标记。

I am trying to loop through some XML and set the value of a string to be equal to a particular nodes contents. The XML looks like:

<RootNode> <SubNode>test<SubNode> <SubNode><ExtraMarkup>some value</ExtraMarkup><SubNode> </RootNode>

Where each sub node can either contain a value or additional XML child nodes. For the first subnode this code works correctly:

for Node := 0 to RootNode.childNodes.length-1 do begin AttrValue := RootNode.childNodes[Node].selectSingleNode('SubNode').Text; // More code here... end;

The problem is when the subnode contains child nodes. I would like the value of AttrValue to be 'test' or '<ExtraMarkup>some value</ExtraMarkup>' as a string.

If instead of text I get the XML attribute the markup is not preserved.

最满意答案

我想你可能想要“IXmlNode.NodeValue”。

这是一个例子:

Function TGlobalConfig.GetXmlItem(CurNode : IXMLNODE; Section : String; var Value : String; Default : String) : Boolean; var ChildNode: IXMLNode; begin if Assigned(CurNode) then begin ChildNode := CurNode.ChildNodes.FindNode(Section); if (ChildNode <> nil) then if VarIsNull(ChildNode.NodeValue) then Value := Default else Value := ChildNode.NodeValue; ... for Node := 0 to RootNode.childNodes.length-1 do begin // Check if the Value stored in SubNode node is xml if (RootNode.childNodes[Node].selectSingleNode('SubNode').hasChildNodes and DealAttributesNode.childNodes[Node].selectSingleNode('SubNode').childNodes[0].hasChildNodes) then begin AttrValue := RootNode.childNodes[Node].selectSingleNode('SubNode').childNodes[0].Xml; end else begin AttrValue := RootNode.childNodes[Node].selectSingleNode('SubNode').Text; end; end;如何从XML中提取子节点值作为字符串(How to extract child node value from XML as string)

我试图循环一些XML并将字符串的值设置为等于特定节点内容。 XML看起来像:

<RootNode> <SubNode>test<SubNode> <SubNode><ExtraMarkup>some value</ExtraMarkup><SubNode> </RootNode>

每个子节点可以包含一个值或其他XML子节点。 对于第一个子节点,此代码正常工作:

for Node := 0 to RootNode.childNodes.length-1 do begin AttrValue := RootNode.childNodes[Node].selectSingleNode('SubNode').Text; // More code here... end;

问题是当子节点包含子节点时。 我希望AttrValue的值为' test '或' <ExtraMarkup>some value</ExtraMarkup> '作为字符串。

如果不是文本而是获取XML属性,则不会保留标记。

I am trying to loop through some XML and set the value of a string to be equal to a particular nodes contents. The XML looks like:

<RootNode> <SubNode>test<SubNode> <SubNode><ExtraMarkup>some value</ExtraMarkup><SubNode> </RootNode>

Where each sub node can either contain a value or additional XML child nodes. For the first subnode this code works correctly:

for Node := 0 to RootNode.childNodes.length-1 do begin AttrValue := RootNode.childNodes[Node].selectSingleNode('SubNode').Text; // More code here... end;

The problem is when the subnode contains child nodes. I would like the value of AttrValue to be 'test' or '<ExtraMarkup>some value</ExtraMarkup>' as a string.

If instead of text I get the XML attribute the markup is not preserved.

最满意答案

我想你可能想要“IXmlNode.NodeValue”。

这是一个例子:

Function TGlobalConfig.GetXmlItem(CurNode : IXMLNODE; Section : String; var Value : String; Default : String) : Boolean; var ChildNode: IXMLNode; begin if Assigned(CurNode) then begin ChildNode := CurNode.ChildNodes.FindNode(Section); if (ChildNode <> nil) then if VarIsNull(ChildNode.NodeValue) then Value := Default else Value := ChildNode.NodeValue; ... for Node := 0 to RootNode.childNodes.length-1 do begin // Check if the Value stored in SubNode node is xml if (RootNode.childNodes[Node].selectSingleNode('SubNode').hasChildNodes and DealAttributesNode.childNodes[Node].selectSingleNode('SubNode').childNodes[0].hasChildNodes) then begin AttrValue := RootNode.childNodes[Node].selectSingleNode('SubNode').childNodes[0].Xml; end else begin AttrValue := RootNode.childNodes[Node].selectSingleNode('SubNode').Text; end; end;