使用最新的NativeXml(v4.03 svn),是否可以使用前面和后面的空白获取元素的值而无需迭代地检查类型?

时间:2012-07-06 11:48:37

标签: xml delphi whitespace nativexml

如下所示,最新的NativeXml(v4.03 svn)将元素的值视为三个部分:前面的空白,char数据和尾随空白。因此,可以迭代地检查元素的子节点以获得具有前导和尾随空白的值。但是,对于传统的NativeXml(v3.10),这不是必需的,因此我想知道什么是首选方式(可能没有这样的迭代检查/字符串连接)来执行此操作?

示例XML文档

<?xml version="1.0" encoding="UTF-8" ?>
<CDXML><s>beforeLineBreak
</s></CDXML>

示例XML文档

<?xml version="1.0" encoding="UTF-8" ?>
<CDXML><s>
afterLineBreak</s></CDXML>

使用NativeXml v3.10的示例代码,不需要迭代检查

procedure XXX310;
var
  element: TXmlNode; 
  elementType: TElementType;
begin
  elementType := element.ElementType;
  element.ElementType := xeCharData;
  ... element.ValueAsString ...
  element.ElementType := elementType;      
end;

NativeXml v3.10(完整方法体)的相关源代码

function TXmlNode.GetValueAsString: UTF8String;
begin
  if FElementType = xeNormal then
    Result := UnEscapeString(sdUTF8Trim(FValue))
  else
    Result := UnEscapeString(FValue);
end;

使用需要迭代检查的NativeXml v4.03 svn的示例代码

procedure XXX403;
var
  tempString: String;
  element: TXmlNode; // actually TsdElement
begin
  tempString := '';
  for I := element.DirectNodeCount to element.NodeCount - 1 do
    if element.Nodes[I] is TsdCharData then
       tempString := tempString + (element.Nodes[I] as TsdCharData).Value;

  ... tempString ...
end;

NativeXml v4.03 svn的相关源代码(不完整类型声明或方法体)

unit NativeXml;
interface

  // TXmlNode is the ancestor for all nodes in the xml document. See TsdElement
  // for the elements, TsdAttribute for the attributes.
  TXmlNode = class(TDebugPersistent)
  Public
    // The value of the node. For elements this is the element value (based on
    // first chardata fragment), for attributes this is the attribute value. The
    // string is encoded as UTF8. Use ToWide(Node.Value) or Node.ValueUnicode
    // to get a UnicodeString compatible with "unicode" windows methods.
    property Value: Utf8String read GetValue write SetValue;
  end;

  // Node representing an xml element.
  TsdElement = class(TsdContainerNode)
  protected
    // parses the value in descendants TsdElement and TsdDocType
    procedure ParseIntermediateData(P: TsdXmlParser); override;
  end;

implementation

function TsdElement.GetValue: Utf8String;
begin
  // Return the value of the CharData subnode designated by the parser
  if (FValueIndex >= 0) and (FValueIndex < FNodes.Count) then
  begin
    // chardata value at FValueIndex
    // This calls TsdCharData.GetValue(),
    // then TsdCharData.GetCoreValue().
    Result := FNodes[FValueIndex].Value;

    // do un-normalisation if mac/windows
    if GetEolStyle <> esLF then
      Result := sdUnNormaliseEol(Result, GetEolStyle);

  end else
    // default value
    Result := '';
end;

procedure TsdElement.ParseIntermediateData(P: TsdXmlParser);  
begin  
  CharDataString := sdTrim(S, PreString, PostString);

  if GetPreserveWhiteSpace and (Length(PreString) > 0) then
  begin
    WhiteSpaceNode := TsdWhiteSpace.Create(TNativeXml(FOwner));
  end;

  if length(CharDataString) > 0 then
  begin
    // Insert CharData node
    CharDataNode := TsdCharData.Create(TNativeXml(FOwner));
  end;  

  if GetPreserveWhiteSpace and (Length(PostString) > 0) then
  begin
    WhiteSpaceNode := TsdWhiteSpace.Create(TNativeXml(FOwner));
  end;
end;

end.  

0 个答案:

没有答案