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

编程入门 行业动态 更新时间:2024-10-24 00:22:00
如何从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;

更多推荐

本文发布于:2023-08-07 23:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466420.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   字符串   XML   extract   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!