5

As shown below, the latest NativeXml (v4.03 svn) sees the element's value as three parts: the preceding blank, the char data, and the trailing blank. Therefore, one could check the element's child nodes iteratively to get the value with preceding and trailing blanks. However, this isn't necessary with legacy NativeXml (v3.10), and I thus wonder what is the preferred way (perhaps without such iteratively checking/string concatenation) to do this?

sample XML document

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

sample XML document

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

sample code using NativeXml v3.10 that doesn't need iteratively checking

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

related source code of NativeXml v3.10 (complete method body)

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

sample code using NativeXml v4.03 svn that needs iteratively checking

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;

related source code of NativeXml v4.03 svn (incomplete type declaration or method body)

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.  
mjn
  • 36,362
  • 28
  • 176
  • 378
SOUser
  • 3,802
  • 5
  • 33
  • 63
  • 2
    I think this is a bug in NativeXML. I'd report it as such with them, and await their reaction. – Jeroen Wiert Pluimers Oct 03 '12 at 07:49
  • I used NativeXML for years with great success, but last year when I moved past Delphi 2007 and needed to upgrade we ran into a number of issues on the new versions. At the time the developer did not have any time to work on the component (this may have changed), so we moved to OXML (http://www.kluug.net) – Mark Elder Jul 09 '15 at 21:07
  • 1
    @JeroenWiertPluimers The "bug" is fixed in latest version v3.32 downloaded from the official website ! :) – SOUser Dec 21 '16 at 06:51
  • so you downgrade from 4.03 to 3.32? – Jeroen Wiert Pluimers Dec 21 '16 at 08:25
  • @JeroenWiertPluimers Apparently the author changed his versioning scheme. The latest v3.32 looks like old v4 but with bug-fixes and future maintenance. – SOUser Dec 21 '16 at 09:10

0 Answers0