Solved

Port Delphi /Pascal Code to C++  (Visual C++)

Posted on 2008-06-21
4
839 Views
Last Modified: 2013-11-23
I'm using this Lib in a Project and want/must utilize this code now from C++,
what is a good way to port this code to C++.


unit xmlUtilV2;
{******************************************************************************
 *  -->>  see http://www.efpage.de/DelphiXML.html
 *            http://msdn2.microsoft.com/en-us/library/ms757878.aspx
 *
 *  MSXML kapselt die Typelibary zur "MSXML.DLL",
 *  Internetexplorer ab Version 5.0 verfügbar
 *
 *            make msxml data handling more conveniant
 *            source from the Internet
 *
 *
 *
 ******************************************************************************}
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, msxml, StdCtrls, ComCtrls;
 
const  CodePage = 'Windows-1252';
 
    function nodetype(const n: IxmlDomNode): cardinal;
    function GetNodeText(const n0: IXMLDomNode;const  tag: string): string;
    function GetNodeInt(const XmlNode: IXmlDomNode;const  tag: string): integer;
    function GetNodeDouble(const XmlNode: IXmlDomNode;const  tag: string): double;
    function GetNodeAttribute(const n0: IXMLDomNode;const  tag: string): string;
    procedure GetNodeTextList(const n0: IXMLDomNode;const  tag: string; const result: TstringList);
    function CreateXmlDoc(const RootNodeName: string = 'ROOT'): IXmlDomDocument;
    procedure ClearNode(const node: IXMLDomNode);
    procedure copyDoc(xml,xml2: IxmlDomDocument);
    Function CreateNode(node: IXMLDomNode; nodeName: string): IXMLDomNode;
    Function CreateSingleNode(const node: IXMLDomNode;const  nodeName: string): IXMLDomNode;
    Function CreateText(const node: IXMLDomNode;const  text: string): IXMLDomNode;
    Function CreateTextNode(const node: IXMLDomNode;const  nodeName, text: string): IXMLDomNode;
    function IsTextNode(const node: IXMLDomNode): boolean;
 
    Function CreateSingleTextNode(const node: IXMLDomNode; const  nodeName, text: string): IXMLDomNode;
    Function CreateNodeAttribute(const node: IXMLDomNode;const  attrib, text: string): IXMLDomNode;
    Function CreateSingleAttribute(const node: IXMLDomNode;const  attrib, text: string): IXMLDomNode;
 
    function SelectOrCreateSubNode(const node: IXMLDomNode;const  subnodeName: string): IXMLDomNode;         overload;
    function SelectOrCreateTextNode(const node: IXMLDomNode;const  TextnodeName, Text: string): IXMLDomNode;     overload;
 
 
type
   EValue = class(Exception);
 
implementation
(*---------------------------------------------------------------------
rval: string in Double wandeln ohne Fehlermeldung
      Bei Fehler Rückgabe 0
---------------------------------------------------------------------*)
function rval(const s: string): Double;
var  E: Integer;
begin
{$R-}
    Val(S, Result, E);
    if E<>0 then result := 0;
{$R+}
end;
(*---------------------------------------------------------------------
ival: string in Integer wandeln;
---------------------------------------------------------------------*)
function ival(const s: string): integer;
begin
   result := round(rval(s));
end;
 
 
(************************************************************************
 
				   XML-Dokument neu erzeugen
 
************************************************************************)
 
{ -------------------------------------------------
  Neues Dokument erzeugen und einen Basisknoten mit Namen RootNodeName anhängen
  Wenn ein leerer Name übergeben wird wird kein Basisknoten erzeugt.
  Wenn kein Parameter angegeben wird, dann wird ein Knoten Root erzeugt.
  -------------------------------------------------}
function CreateXmlDoc(const RootNodeName: string = 'ROOT'): IXmlDomDocument;
var
    root: IXMLDomElement;
    PI: IXMLDomProcessingInstruction;
begin
    result := CoDOMDocument.create;
    result.preserveWhiteSpace := True;
    PI := result.CreateProcessingInstruction('xml',
          Format('version="1.0" encoding="%s"', [codepage]));
    result.AppendChild(PI);
    if RootNodeName<>'' then
    begin
        root := result.CreateElement(RootNodeName);
        result.AppendChild(root);
    end;
end;
 
(************************************************************************
 
				   XML-Nodes erzeugen
 
************************************************************************)
 
{ -------------------------------------------------
  Liefert direkt den Typ eines Knotens
  -------------------------------------------------}
function nodetype(const n: IxmlDomNode): cardinal;
begin
    if n.hasChildNodes then
        result :=   n.firstChild.nodeType
    else
        result :=   Node_Invalid;
end;
 
{ -------------------------------------------------
  Alle Subnodes und Attribute löchen
  -------------------------------------------------}
procedure ClearNode(const node: IXMLDomNode);
var s: string;
    parent,newchild: IXMLDomNode;
begin
    s := node.nodeName;
 
    if node.parentNode=Nil then
        exit;
 
    newchild := node.ownerDocument.createElement(s);
    parent := node.parentNode;
    parent.replaceChild(newchild,node);
    newchild := node;
    newchild := Nil;
end;
 
procedure copyDoc(xml,xml2: IxmlDomDocument);
var  node: IxmlDomNode;
begin
    ClearNode(xml2.documentElement);
    node := xml.documentElement.firstChild;
    while node <> nil do
    begin
        xml2.documentElement.appendChild(node.cloneNode(true));
        node := node.nextSibling
    end;
end;
{ -------------------------------------------------
  Knoten erzeugen
  Klammerausdrücke im Namen erzeugen ein Attribut Nr.
  -------------------------------------------------}
Function CreateNode( node: IXMLDomNode; nodeName: string): IXMLDomNode;
var Id: IXMLDomNode;
    attr: string;
begin
   if Node=Nil then
   begin
       result := Nil;
       exit;
   end;
   Id := node.ownerDocument.createElement(NodeName);
   node := node.appendChild(Id);
   result := Node;
end;
 
 
{ -------------------------------------------------
  Knoten erzeugen, wenn er noch nicht existiert
  -------------------------------------------------}
Function CreateSingleNode(const node: IXMLDomNode;const nodeName: string): IXMLDomNode;
var Id: IXMLDomNode;
begin
   if Node=Nil then
   begin
       result := Nil;
       exit;
   end;
   result := node.SelectSingleNode(NodeName);
   if Result = Nil then
   begin
       Id := node.ownerDocument.createElement(NodeName);
       result := node.appendChild(Id);
   end;
end;
 
{ -------------------------------------------------
  Textinhalt eines Knoten erzeugen
  -------------------------------------------------}
Function CreateText(const node: IXMLDomNode;const  text: string): IXMLDomNode;
begin
   if Node=Nil then
   begin
       result := Nil;
       exit;
   end;
   node.appendChild(node.ownerDocument.createTextNode(text));
   result := Node;
end;
 
{ -------------------------------------------------
  Prüfen, ob es sich um einen reinen Textknoten handelt
  -------------------------------------------------}
function IsTextNode(const node: IXMLDomNode): boolean;
var child: IXMLDomNode;
begin
 
    child := node.selectSingleNode('*');
    result := child = Nil;
end;
 
{ -------------------------------------------------
  Knoten mit Textinhalt erzeugen
  -------------------------------------------------}
Function CreateTextNode(const node: IXMLDomNode; const nodeName, text: string): IXMLDomNode;
begin
    result := CreateNode(node,nodename);
    CreateText(result,text);
end;
 
{ -------------------------------------------------
  Knoten mit Textinhalt nur erzeugen, wenn er noch nicht existiert
  sonst den aktuellen Wert überschreiben
  -------------------------------------------------}
Function CreateSingleTextNode(const node: IXMLDomNode; const nodeName, text: string): IXMLDomNode;
begin
    result := node.selectSingleNode(nodeName);
    if result = Nil then
    begin
        result := CreateNode(node,nodename);
        CreateText(result,text);
    end
    else
        result.text := text;
end;
 
{ -------------------------------------------------
  Knoten mit Textinhalt erzeugen
  -------------------------------------------------}
{Function CreateTextNode(doc: IXMLDomDocument; node: IXMLDomNode; nodeName, text: string): IXMLDomNode;
var Id,tx: IXMLDomNode;
begin
   if Node=Nil then
   begin
       result := Nil;
       exit;
   end;
   Id := doc.createElement(NodeName);
   tx := doc.createTextNode(text);
   node := node.appendChild(Id);
   node.appendChild(tx);
   result := Node;
end;
}
Function CreateNodeAttribute(const node: IXMLDomNode; const attrib, text: string): IXMLDomNode;
var    attr: IXmlDomAttribute;
begin
   attr := node.ownerDocument.createAttribute(attrib);
   node.attributes.setNamedItem(attr);
   attr.value := text;
end;
 
Function CreateSingleAttribute(const node: IXMLDomNode; const attrib, text: string): IXMLDomNode;
var    attr: IXmlDomNode;
begin
   attr := node.attributes.getNamedItem(attrib);
   if attr <> Nil then
       attr.text := text
   else
       CreateNodeAttribute(node, attrib, text);
       
end;
 
{ -------------------------------------------------
   Text aus node lesen
  -------------------------------------------------}
function GetNodeText(const n0: IXMLDomNode; const tag: string): string;
var n1: IXMLDomnode;
    i: integer;
begin
    if n0=Nil then begin
        result := '';
        exit;
    end;
    n1 := n0.SelectSingleNode(tag);
    if n1=Nil then
        result := ''
    else
    begin
        result := n1.text;
        repeat
        i := pos('?',result);
        if i>0 then
            result[i] := '-';
        until i<=0;
    end;
end;
 
{ -------------------------------------------------
  Werte aus nodes holen
  Bei fehlendem Eintrag oder leerem Node Rückgabe 0
  -------------------------------------------------}
function GetNodeInt(const XmlNode: IXmlDomNode; const tag: string): integer;
begin
    result := round(GetNodeDouble(XmlNode,tag));
end;
 
function GetNodeDouble(const XmlNode: IXmlDomNode; const tag: string): double;
begin
    if XmlNode<>Nil then
        result := rval(GetNodeText(XmlNode,tag))
    else
        result := 0;
end;
{ -------------------------------------------------
   Attribut lesen
  -------------------------------------------------}
function GetNodeAttribute(const n0: IXMLDomNode; const tag: string): string;
var at: IxmlDomNode;
begin
    result := '';
    if n0=Nil then exit;
    if n0.attributes.length=0 then exit;
    at := n0.attributes.getNamedItem(tag);
    if at=Nil then exit;
    result := at.text;
end;
{ -------------------------------------------------
  Liste von Werten aus einem Knoten lesen
  -------------------------------------------------}
procedure GetNodeTextList(const n0: IXMLDomNode; const tag: string; const result: TstringList);
var n1: IXMLDomnodeList;
    i: integer;
begin
    result.Clear;
    if n0=Nil then begin
        exit;
    end;
    n1 := n0.SelectNodes(tag);
    if n1<>Nil then
    begin
        for i := 0 to n1.length-1 do
            result.Add(n1.item[i].nodeTypedValue);
    end;
end;
{ -------------------------------------------------
  Data-Node suchen nach Id oder ggfs erzeugen
  -------------------------------------------------}
function SelectOrCreateSubNode(const node: IXMLDomNode; const subnodeName: string): IXMLDomNode;
var a: string;
begin
    result := node.selectSingleNode(subnodeName);
 
    if result=Nil then
    begin
        result := node.appendChild(node.ownerDocument.createElement(subnodeName));
        if a <> '' then
            CreateNodeAttribute(result,'Nr',a);
    end;
end;
 
{ -------------------------------------------------
  'Text-Node suchen nach Id oder ggfs erzeugen
  -------------------------------------------------}
function SelectOrCreateTextNode(const node: IXMLDomNode; const TextnodeName, Text: string): IXMLDomNode;     overload;
begin
    result := SelectOrCreateSubnode(node,TextNodeName);
    result.text := Text;
end;
 
 
end.

Open in new window

0
Comment
Question by:BdLm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Expert Comment

by:Mamouri
ID: 21837427
Why do you want to port this code to C++. Instead use Microsoft XML. It's more standard and supported by Microsoft, easier to use and there are lot's of samples for that on the web.
0
 
LVL 8

Author Comment

by:BdLm
ID: 21844374
I use MSXML and the XML Manager CLass from <http://www.codeguru.com/cpp/data/data-misc/xml/article.php/c8287/> )
Beside the performance issue (<http://www.experts-exchange.com/Programming/Languages/CPP/Q_23244509.html?cid=239> )
and many other trouble with this lib (http://www.experts-exchange.com/Programming/Languages/CPP/Q_23244509.html)  or
( http://www.experts-exchange.com/Programming/Languages/CPP/Q_23258772.html) . now
I  gave up to improve this code. MSXML works perfect with this Pascal Unit and Delphi, therefore my next approach for a conventiant
solution would be to transfer this code to C++.
0
 
LVL 3

Accepted Solution

by:
Mamouri earned 500 total points
ID: 21844483
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert MFC::CString to UTF8 wchar_t* 10 379
Arduino EDI - Programming language 3 114
Header of docx file 17 130
Netbeans and org.apache.commons.lang3 issue 3 24
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question