EldoS | Feel safer!

Software components for data protection, secure storage and transfer

find nodes with same names

Also by EldoS: CallbackProcess
A component to control process creation and termination in Windows and .NET applications.
#33901
Posted: 07/06/2015 15:16:22
by Victor Villegas (Standard support level)
Joined: 02/27/2012
Posts: 18

how I can find a node with a certain name and the following nodes with the same name in an XML file?
#33905
Posted: 07/06/2015 16:19:13
by Dmytro Bogatskyy (EldoS Corp.)

Thank you for contacting us.


There are several possibilities how to search for nodes:
1. To use DOM method TElXMLDOMNode.FindNode() to search for the first occurrence of the node by its node name. See: https://www.eldos.com/documentation/sb...dnode.html
2. To use XPath expression to select one or more nodes using TElXMLDOMNode.SelectNodes() method. See: https://www.eldos.com/documentation/sb...nodes.html
3. To use helper functions from SBXMLUtils unit such as FindElementById() and FindElementByName() to search for the first occurrence of the node by its Id attribute or element's local name and namespace URI.
4. To use TElXMLDOMNode.FirstChild/LastChild and NextSibling/PreviousSibling properties to traverse a DOM tree.
#33921
Posted: 07/07/2015 09:29:16
by Victor Villegas (Standard support level)
Joined: 02/27/2012
Posts: 18

Hello, I have problems to travel the tree, because only through the first node and exits.
The code I use is as follows:

Code
x:= x.FirstChild;
  while  Assigned(X) do
  begin
    if x.NodeName = 'DTE' then
      ItemDte:= i;
    if x.NodeName = 'Documento' then
    begin
      NumURI:= x.NodeValue;
      Firmar(i);
    end;
    x := x.NextSibling;
    //FXMLDocument.NextSibling;
  end;


and the XML file is as follows:

Code
<EnvioDTE xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioDTE_v10.xsd" version="1.0">
<SetDTE ID="SetDoc">
<Caratula version="1.0">
<RutEmisor>96592860-K</RutEmisor>
<RutEnvia>10512258-6</RutEnvia>
<RutReceptor>60803000-K</RutReceptor>
<FchResol>2011-06-13</FchResol>
<NroResol>0</NroResol>
<TmstFirmaEnv>2011-06-13T15:35:00</TmstFirmaEnv>
<SubTotDTE>
<TpoDTE>33</TpoDTE>
<NroDTE>1</NroDTE>
</SubTotDTE>
</Caratula>
<DTE version="1.0">
<Documento ID="F41T33">
<Encabezado>
<IdDoc>
<TipoDTE>33</TipoDTE>
<Folio>41</Folio>
<FchEmis>2015-04-23</FchEmis>
</IdDoc>
<Emisor>
<RUTEmisor>96592860-K</RUTEmisor>
<RznSoc>DHARMA USAHA S.A.</RznSoc>
<GiroEmis>COMPUTACION</GiroEmis>
<Acteco>726000</Acteco>
<DirOrigen>AV. NUEVA PROVIDENCIA 2214 OF 166</DirOrigen>
<CmnaOrigen>PROVIDENCIA</CmnaOrigen>
<CiudadOrigen>SANTIAGO</CiudadOrigen>
</Emisor>
<Receptor>
<RUTRecep>02672948-3</RUTRecep>
<RznSocRecep>CARLOS ACU&NtildeA AGUAYO</RznSocRecep>
<GiroRecep>CONTABILIDAD / IMPRENTA</GiroRecep>
<DirRecep>AVDA PONIENTE 1728</DirRecep>
<CmnaRecep>MOLINA</CmnaRecep>
<CiudadRecep>M0LINA</CiudadRecep>
</Receptor>
<Totales>
<MntNeto>279005</MntNeto>
<TasaIVA>19</TasaIVA>
<IVA>53011</IVA>
<MntTotal>332016</MntTotal>
</Totales>
</Encabezado>
<Detalle>
<NroLinDet>1</NroLinDet>
<NmbItem>CAJON AFECTO</NmbItem>
<QtyItem>129</QtyItem>
<PrcItem>1269</PrcItem>
<MontoItem>163701</MontoItem>
</Detalle>
<Detalle>
<NroLinDet>2</NroLinDet>
<NmbItem>RELLENO AFECTO</NmbItem>
<QtyItem>56</QtyItem>
<PrcItem>2059</PrcItem>
<MontoItem>115304</MontoItem>
</Detalle>
<Referencia>
<NroLinRef>1</NroLinRef>
<TpoDocRef>SET</TpoDocRef>
<FolioRef>1</FolioRef>
<FchRef>2011-05-06</FchRef>
<CodRef>1</CodRef>
<RazonRef>Caso 363454-1</RazonRef>
</Referencia>
<TED version="1.0">
<DD>
<RE>96592860-K</RE>
<TD>33</TD>
<F>41</F>
<FE>2015-04-23</FE>
<RR>02672948-3</RR>
<RSR>CARLOS ACU&NtildeA AGUAYO</RSR>
<MNT>332016</MNT>
<IT1>RELLENO AFECTO</IT1>
<CAF version="1.0">
<DA>
<RE>96592860-K</RE>
<RS>DHARMA USAHA S A</RS>
<TD>33</TD>
<RNG>
<D>41</D>
<H>41</H>
</RNG>
<FA>2015-02-25</FA>
<RSAPK>
<M>1t/cp7nQetdrxFDwot+rUuAwWexbBO6Lkzx/hIUberG2ToCcKfzoCSfATj8hqcRueoF04vWlTWYPf/2/KwpyTQ==</M>
<E>Aw==</E>
</RSAPK>
<IDK>100</IDK>
</DA>
<FRMA algoritmo="SHA1withRSA">CWNHx4f/066n6dwl2vsLPAgoMTmZaLVsuikcP5yhS0nJMlW/DYnPEub5YXN6Rt/j3kkov23wHDTBKr3U1Epb9A==</FRMA>
</CAF>
<TSTED>2015-07-07T09:55:32</TSTED>
</DD>
<FRMT algoritmo="SHA1withRSA">ui3TIdB8iT8wP3Qs4dJPCMYH1ptD8gSZGo/F614Re5TwUPxB59E4Jrc7uq5BPEJ+ObqJbqbbRiUWEQ9E3tb0/g==</FRMT>
</TED>
<TmstFirma>2015-07-07T09:55:32</TmstFirma>
</Documento>
</DTE>
</SetDTE>
</EnvioDTE>


The problem to go through the code reads "#text", then "EnvioDTE" and exits, does not pass through other nodes.
It will read everything as a single node?
#33922
Posted: 07/07/2015 09:42:05
by Dmytro Bogatskyy (EldoS Corp.)

Hi,

Quote
The problem to go through the code reads "#text", then "EnvioDTE" and exits, does not pass through other nodes.
It will read everything as a single node?

It is a tree. "EnvioDTE" node has own child nodes, so you need to call your find method recursively.
Here it is the implementation of TElXMLDOMNode.FindNode() method that you can use as a reference:
Code
function TElXMLDOMNode.FindNode(const aNodeName: XMLString; Deep : Boolean): TElXMLDOMNode;
var
  Child: TElXMLDOMNode;
begin
  Result := nil;
  Child := FirstChild;
  while Assigned(Child) do
  begin
    if Child.NodeName = aNodeName then
    begin
      Result := Child;
      Break;
    end;

    if Deep then
    begin
      Result := Child.FindNode(aNodeName, Deep);
      if Assigned(Result) then
        Break;
    end;

    Child := Child.NextSibling;
  end;
end;
#33928
Posted: 07/07/2015 12:56:27
by Victor Villegas (Standard support level)
Joined: 02/27/2012
Posts: 18

ok but as I get the value of the node?
For example, the node
<Documento ID = "F41T33">
I want to get the value "F41T33"

I tried it with x.NodeValue, also with XMLDOMElement (x) .NodeValue, but none gives me that value.

And the other, how do I know what number is that node in the tree. For example, the node <Documento ID = "F41T33"> is number 14 in the tree?
#33931
Posted: 07/07/2015 15:15:49
by Dmytro Bogatskyy (EldoS Corp.)

Hi,
Quote
<Documento ID = "F41T33">
I want to get the value "F41T33"

This value is the ID attribute value. You can use TElXMLDOMElement.AttribStrings indexed property to access this value. See: https://www.eldos.com/documentation/sb...rings.html

Quote
And the other, how do I know what number is that node in the tree. For example, the node <Documento ID = "F41T33"> is number 14 in the tree?

The DOM doesn't have any definition for the node index (number) in the tree. You would need to calculate it by yourself.
Also by EldoS: CallbackProcess
A component to control process creation and termination in Windows and .NET applications.

Reply

Statistics

Topic viewed 1352 times

Number of guests: 2, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!