Delphi 7 - manipulate XML file using OmniXML library
Asked Answered
G

1

5

I have a big XML file, around 50 megs and I trying to use the OmniXML library to manipulate the XML document.

I didn't understand the demos in OmniXML...

The XML file have a structure like this:

<rollercoaster build="0.1 (Dec 30 2010)" debug="no">
    <settings name="roller coaster" sourcefile="rolcost.pas">
        <description>Roller Coaster admin function</description>
        <year>2010</year>
        <manufacturer>ArtTeck</manufacturer>
            <sears name="sears.uk" size="1024"  mda="87117ba5082cd7a615b4ec7c02dd819" region="england" set1="25d"/>
    <sears name="sears.dk" size="1056"  mda="326dbbf94c6fa2e96613dedb53702f8" region="denmark" set1="25d"/>
    <sears name="sears.gr" size="6802"  mda="01b4c38108d9dc4e48da4f8d5821377" region="greece" set1="65d"/>
    </settings>
    <settings name="roller coaster2" sourcefile="rolcost2.pas">
        <description>Roller Coaster user function</description>
        <year>2010</year>
        <manufacturer>ArtTeck</manufacturer>
    </settings>...... and goes on
</rollercoaster>

The things I want to know are:

  • How to make the loop display the nodes and child nodes in a StringGrid.
  • How can I get the data from a single node (for debug purposes).
  • How can I select he child node of the settings node?
  • How can I take the list of the same node and how to separate the node "sears"...?
Guiana answered 25/5, 2011 at 9:22 Comment(7)
use TXMLDocument provided in delphi 7 or make your own praserCopilot
@opc0de: The question specifically asks about OmniXML, so recommending TXMLDocument or rolling another parser are not useful.Panelboard
@opc0de: No argument. Just pointing out you weren't addressing the question asked.Panelboard
What do you mean with 'take the list of the same node' and what do you mean with 'separate the node'? Maybe you should just the output you would expect from those two functions.Secularity
My english again.... i mean that i have the same node "sears" how am i suppose to know which sears node is... how can i control that bcs "sears" node exist in the next "settings" node and goes on and on... Sorry about my bad english...Guiana
Sorry, still don't understand.Secularity
Ok i'll try again.... I have 3 nodes name "sears" and have the same attr but values to return how i get access of their values and how i add it in a string list sorry if i tire you... Thanks for your help...Guiana
S
8
uses
  OmniXML,
  OmniXMLUtils;

procedure TForm28.FormCreate(Sender: TObject);
var
  descNode: IXMLNode;
  iNode   : integer;
  node    : IXMLNode;
  nodeList: IXMLNodeList;
  xml     : IXMLDocument;
begin
  xml := CreateXMLDoc;
  if XMLLoadFromFile(xml, 'c:\0\roller.xml') then begin
    // node enumeration in D2005+
    //for node in XMLEnumNodes(xml, '/rollercoaster/settings') do
    //  lbLog.Items.Add(GetNodeTextStr(node, 'description'));
    // node enumeration in D7 and older
    nodeList := xml.SelectNodes('/rollercoaster/settings');
    for iNode := 0 to nodeList.Length - 1 do begin
      node := nodeList.Item[iNode];
      lbLog.Items.Add(GetNodeTextStr(node, 'description'));
    end;
    // selecting a single node with specified attribute name
    node := xml.SelectSingleNode('/rollercoaster/settings[@name="roller coaster2"]');
    // accessing subnode text
    lbLog.Items.Add(GetNodeTextStr(node, 'description'));
    // accessing subnode text, alternative way
    descNode := node.SelectSingleNode('description');
    lbLog.Items.Add(GetNodeText(descNode));
    // accessing node attribute
    lbLog.Items.Add(GetNodeAttrStr(node, 'sourcefile'));
  end;
end;
Secularity answered 25/5, 2011 at 11:18 Comment(13)
+1. I would have added a little explanation: "OmniXML objects support iteration, so you can use them in a for loop on versions of Delphi that support for..in syntax". This code only works in Delphi 2005. A second variant that works in D7 would be handy for the OP.Cloven
I meant to say, "only works in Delphi 2005, and up, including Delphi XE".Cloven
Warren, you're completely right. I forgot about the "Delphi 7" part. I'll update the demo.Secularity
I did it for you! Check below. Feel free to copy it.Cloven
I implemented it all by myself ;)Secularity
Okay, now it works in D7, Great! My answer is pointless now, deleted.Cloven
Can you help me how to select the child nodes of the settings node?Guiana
gabr thank you for the alternative way... but do you know how to get the text from a child node of the "settings" node ?Guiana
I think i am not sent the question right or not understand ... i just want the result of the "sourcefile" of the "settings" node... Thank you again for your help...Guiana
azrael11: If you want to read the attribute value, call one of GetNodeAttr* functions. I've updated my answer to show how to do it.Secularity
Tell me can you tell me if there is a chance to get the process of load xml to a TProgressBar bcs the file take one minute to load in my machine P4... Thank you again Thank you very muchGuiana
azrael11: No, you can't do that.Secularity
gabr i update my question can you help me? I think it is the last think i stack.... Thank you very much...Guiana

© 2022 - 2024 — McMap. All rights reserved.