Solved

Selecting a particular Node value in a XML Document

Posted on 2013-11-22
17
2,798 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 30

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 30

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 30

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 30

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

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

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 30

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 30

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 30

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

14 Experts available now in Live!

Get 1:1 Help Now