Solved

Selecting a particular Node value in a XML Document

Posted on 2013-11-22
17
2,875 Views
Last Modified: 2013-11-22
Using Delphi Xe-5 and the following XML

<?xml version="1.0" encoding="utf-8" ?>

<Config Context="Manage">
    <Selector Description="This group determines how we display items.">
        <Type Description="This is our selector">Browse</Type>

Open in new window


How do I get the value "Browse" from node "Type" under node "Selector"?

var
  Selector: IXMLNode;

 XMLDoc.Active;
     XMLDoc.LoadFromXML(Connector.ResponseXML);

     try
      Selector:= XMLDoc.DocumentElement;
      //

     finally
       XMLDoc.Active := False;
     end;

Open in new window


thanx
0
Comment
Question by:livestuff
  • 9
  • 8
17 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 39670215
Try this:

var
RootNode, ConfigNode, SelectorNode, TypeNode: IXMLNode;
I, X, Y: Integer;
Value: string;
begin

	XMLDoc.Active;
    XMLDoc.LoadFromXML(Connector.ResponseXML);
    try
		RootNode := XMLDocument1.DocumentElement;
		for I := 0 to RootNode.ChildNodes.Count - 1 do
		begin
		  ConfigNode := RootNode.ChildNodes[I];
		  for X := 0 to configNode.ChildNodes.Count - 1 do
		  begin
			SelectorNode := configNode.ChildNodes[X];
			for Y :=  0 to SelectorNode.ChildNodes.Count - 1 do
			begin
			  TypeNode := SelectorNode.ChildNodes[Y];
			  Value := TypeNode.Text;
			end;
		  end;
	   end;
   finally
     XMLDoc.Active := False;
   end;
end;	

Open in new window

0
 
LVL 1

Author Comment

by:livestuff
ID: 39670264
SO, Hmmmm, I think its almost there, but now sure
 RootNode := XMLDoc.DocumentElement;
		for I := 0 to RootNode.ChildNodes.Count - 1 do
		begin
		  ConfigNode := RootNode.ChildNodes[I];
		 for X := 0 to configNode.ChildNodes.Count - 1 do
		  begin
			SelectorNode := configNode.ChildNodes[X];
      ShowMessage(SelectorNode.Text);
			
	   end;
     finally
       XMLDoc.Active := False;
     end;

Open in new window


Running this and using a show message at the Selctor node level, I get two values diplayed and the second one is the entire xml document string
0
 
LVL 1

Author Comment

by:livestuff
ID: 39670285
If I do this
RootNode := XMLDoc.DocumentElement;
ShowMessage(RootNode.Text);

Open in new window

I get the message that "Element GetContext does not contain a single text node"

intereestingly, if I look at my raw response XML, it looks like this

<?xml version="1.0" encoding="UTF-8"?>
<GetContext>
  <HierarchyPosition>000020000</HierarchyPosition>
  <LayoutXMLData>  &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;

&lt;Config Context=&quot;Manage&quot;&gt;
    &lt;Selector Description=&quot;This group determines how we display items.&quot;&gt;
        &lt;Type Description=&quot;This is our selector&quot;&gt;Browse&lt;/Type&gt;
        &lt;Item_Name Description=&quot;This is the DT we are going to display for the item&quot;&gt;Formatted Name&lt;/Item_Name&gt;
        &lt;Order&gt;
            &lt;Rank&gt;Asc&lt;/Rank&gt;
        &lt;/Order&gt;
        &lt;Selector_Search&gt;
            &lt;DataType&gt;Formatted Name&lt;/DataType&gt;
            &lt;DataType&gt;Data Mapping__ID&lt;/DataType&gt;
        &lt;/Selector_Search&gt;
    &lt;/Selector&gt;
    &lt;Context_Collection Description=&quot;The Tabs to draw.&quot;&gt;Manage&lt;/Context_Collection&gt;
    &lt;Functionality&gt;
        &lt;New Description=&quot;We can add items&quot;&gt;Y&lt;/New&gt;
        &lt;Duplicate Description=&quot;Whether we can duplicate items&quot;&gt;N&lt;/Duplicate&gt;
        &lt;Link Description=&quot;Whether we can Link items&quot;&gt;N&lt;/Link&gt;
        &lt;Move Description=&quot;Whether we can move items&quot;&gt;Y&lt;/Move&gt;
        &lt;Show_Parent&gt;N&lt;/Show_Parent&gt;
        &lt;Merge&gt;Y&lt;/Merge&gt;
        &lt;Archive Description=&quot;We can delete items&quot;&gt;Y&lt;/Archive&gt;
    &lt;/Functionality&gt;
    &lt;Items&gt;
        &lt;ObjectType_Path&gt;[Application]\\Type Hierarchy\\Object\\Information\\Taxonomy Item&lt;/ObjectType_Path&gt;
    &lt;/Items&gt;
    &lt;Move_Settings&gt;
        &lt;Action&gt;UpdateTaxonomyAndItems&lt;/Action&gt;
        &lt;Confirmation&gt;WARNING: This is a major change and may take a long time to reindex. Are you sure you wish to proceed?&lt;/Confirmation&gt;
    &lt;/Move_Settings&gt;
&lt;/Config&gt;</LayoutXMLData>
  <LayoutFormXMLData>  </LayoutFormXMLData>
</GetContext>

Open in new window


is that encoding? Does it need to be decode first.

Cause like I said above

Running your code and using a show message at the Selctor node level, I get two values diplayed . The first is : 000020000  and the second one is the entire xml document string

missing this part

<GetContext>
  <HierarchyPosition>000020000</HierarchyPosition>
  <LayoutXMLData>  &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39670291
Tested folllowing code in Delphi XE3 and it works as expected:

procedure TForm1.Button1Click(Sender: TObject);
var
  RootNode, ConfigNode, SelectorNode, TypeNode: IXMLNode;
  I, X, Y: Integer;
  Value: string;
begin
	XMLDoc.Active;
    XMLDoc.LoadFromFile(ExtractFilePath(Application.ExeName)+'mydoc.xml');
    try
		RootNode := XMLDoc.DocumentElement;
		for I := 0 to RootNode.ChildNodes.Count - 1 do
		begin
		  ConfigNode := RootNode.ChildNodes[I];
		  for X := 0 to configNode.ChildNodes.Count - 1 do
		  begin
			SelectorNode := configNode.ChildNodes[X];
			for Y :=  0 to SelectorNode.ChildNodes.Count - 1 do
			begin
			  TypeNode := SelectorNode.ChildNodes[Y];
			  Value := TypeNode.Text;
        Showmessage(Value);
			end;
		  end;
	   end;
   finally
     XMLDoc.Active := False;
   end;
end;

Open in new window

0
 
LVL 1

Author Comment

by:livestuff
ID: 39670311
I think its the '&lt;' & &gt;' strings in the above XML. Im going to strip them and replace them with '<' and '>' respectively and I will let you know
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39670325
Try to add to your uses clause Soap.HTTPUtil and use

mydoc := HTMLEscape(Connector.response);

declare mydoc as string. This way I think it should work.
0
 
LVL 1

Author Comment

by:livestuff
ID: 39670335
I added Soap.HTTPUtil to uses clause and HTMLEscape is still unidentified
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39670351
Well, in XE3 the function is recognized correctly. Probably it has been moved in another unit: try to search the help for that function. I don't have XE5 :(
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:livestuff
ID: 39670355
ya, i figured and I am looking now
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39670358
You can try this function I found in the web:

function HTMLEncode3(const Data: string): string;
var
  iPos, i: Integer;

  procedure Encode(const AStr: String);
  begin
    Move(AStr[1], result[iPos], Length(AStr) * SizeOf(Char));
    Inc(iPos, Length(AStr));
  end;

begin
  SetLength(result, Length(Data) * 6);
  iPos := 1;
  for i := 1 to length(Data) do
    case Data[i] of
      '<': Encode('&lt;');
      '>': Encode('&gt;');
      '&': Encode('&amp;');
      '"': Encode('&quot;');
    else
      result[iPos] := Data[i];
      Inc(iPos);
    end;
  SetLength(result, iPos - 1);
end;

Open in new window

0
 
LVL 1

Author Comment

by:livestuff
ID: 39670364
ya, i found a ton of them on the web, after i wrote my own alittle function to handle "<", ">", and Quotes

Your  HTMLEscape finally compile for me , but thts not what I need, it looks like that encoded it not decoded it
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39670374
Oooops: then use HtmlDecode (I'm not sure if it is in HTTPUtil or in HTTPApp)
0
 
LVL 1

Author Comment

by:livestuff
ID: 39670375
I have tried three different ones now and they all give me this error (see attached screenhot)
snap1.png
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39670384
Then I would try to apply the function within the loop, line by line. It's not the first time the line <?xml version="1.0"> gives some error. Now I have to leave you but I'll come back tomorrow morning.
Cheers
0
 
LVL 1

Author Comment

by:livestuff
ID: 39670385
OK, tried that one too!  and its Web.HTTPApp :)

But I still get the error i submitted in screenshot. Evidently, it is something in our XML code but I am not 100% sure
0
 
LVL 1

Author Comment

by:livestuff
ID: 39670522
Turns out it was our XML! <smile>
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39670929
Hi. Thanks for points. What your xml had wrong?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi exception 7 63
how to center only a line in richedit? 4 52
When I am typing into a TEdit last word repeats 7 119
delphi parse string to params 3 101
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now