Recent

Author Topic: read XML  (Read 4021 times)

jolesin

  • Newbie
  • Posts: 3
read XML
« on: October 16, 2017, 11:54:28 am »
Hello
I have xml file like this:

Code: Pascal  [Select][+][-]
  1.         <item_list>
  2.             <construction_item>
  3.                 <identification>1</identification>
  4.                 <item_number>1</item_number>
  5.                 <short_description>WINDOW</short_description>
  6.                 <internal>false</internal>
  7.             </construction_item>
  8.             <construction_item>
  9.                 <identification>2</identification>
  10.                 <item_number>5</item_number>
  11.                 <short_description>DOOR</short_description>
  12.                 <internal>false</internal>
  13.             </construction_item>
  14.             <construction_item>
  15.                 <identification>3</identification>
  16.                 <item_number>1</item_number>
  17.                 <short_description>BIG</short_description>
  18.                 <internal>false</internal>
  19.             </construction_item>
  20. .
  21. .
  22. .
  23.         </item_list>
  24.  
  25.  

and I need read some data:

  identification[1]:=1
  item_number[1]:=1
  short_description[1]:=WINDOW

  identification[2]:=2
  item_number[2]:=5
  short_description[2]:=DOOR

  identification[3]:=2
  item_number[3]:=1
  short_description[3]:=BIG
.
.
.
  identification[ i ]:=xxx
  item_number[ i ]:=xxx
  short_description[ i ]:=XXX


please help me !!
« Last Edit: October 16, 2017, 12:12:11 pm by jolesin »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: read XML
« Reply #1 on: October 16, 2017, 12:45:58 pm »
Hi jolesin,

Did you already had a look at the tutorial on XML files on the wiki ?

That should be able to get you going, and in case not then please share which part of the tutorial you did not understand or show some of your code that is causing problems for you.

jolesin

  • Newbie
  • Posts: 3
Re: read XML
« Reply #2 on: October 16, 2017, 01:42:24 pm »
Yes I read this tutorial, but I dont know how prepare part "for" to my problem

node1:=?
for i:=1 to count
.
.
.
end

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: read XML
« Reply #3 on: October 16, 2017, 01:54:18 pm »
Yes I read this tutorial, but I dont know how prepare part "for" to my problem
That is because afaik you can't.

Or to be more specific: not with TXMLDocument/TDomNode. (*)

The internal structure is stored in such a format that 'items' can be accessed by their siblings, children, parent etc.

Ergo, many ways can lead to Rome. This small snippet (to hopefully get you started) is just one of them:
Code: Pascal  [Select][+][-]
  1.     // Read in xml file from disk
  2.     ReadXMLFile(Doc, 'test.data.xml');
  3.     // Retrieve the 'first' construction node
  4.     ThisNode := Doc.DocumentElement.FindNode('construction_item');
  5.     while Assigned(ThisNode) do
  6.     begin
  7.      // process individual children, one for each item in your xml construction structure -> To be inserted by you
  8.  
  9.      //  next sibling of construction_item
  10.      ThisNode := ThisNode.NextSibling;
  11.    end;
  12.  

edit: (*) I just noticed the GetChildNodes list. I'll have a look at that.

edit 2: Ah, the list is just as awkward to use so in my opinion it doesn't change much to the code (snippet again):
Code: Pascal  [Select][+][-]
  1.     // Read in xml file from disk
  2.     ReadXMLFile(Doc, 'test.data.xml');
  3.  
  4.     NodeList := Doc.DocumentElement.ChildNodes;
  5.  
  6.     if Assigned(NodeList) then
  7.     begin
  8.       for i := 0 to NodeList.Count-1 do
  9.       begin
  10.         if NodeList.Item[i].NodeName = 'construction_item' then
  11.         begin
  12.            // process individual children, one for each item in your xml construction structure -> To be inserted by you
  13.         end;
  14.       end;
  15.     end;
  16.  
« Last Edit: October 16, 2017, 02:21:58 pm by molly »

jolesin

  • Newbie
  • Posts: 3
Re: read XML
« Reply #4 on: October 16, 2017, 05:26:36 pm »
Thanks !!!!!!!!!!!!!!
I used first way.
It is work's (but of course  you know) !!!!!!!!!!!!!!!

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: read XML
« Reply #5 on: October 17, 2017, 12:55:04 pm »
thank you for the feedback jolesin.

It is work's (but of course  you know) !!!!!!!!!!!!!!!
I left the child-processing up to your because doing that yourself is the only way to get familiar with xml document processing. Hopefully the obtained experience from that is able to help you exploring things further. Almost everybody in this world knows how to copy-paste  :-*

 

TinyPortal © 2005-2018