Selecting a particular Node value in a XML Document

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
LVL 1
livestuffAsked:
Who is Participating?
 
Marco GasiFreelancerCommented:
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
 
livestuffAuthor Commented:
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
 
livestuffAuthor Commented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Marco GasiFreelancerCommented:
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
 
livestuffAuthor Commented:
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
 
Marco GasiFreelancerCommented:
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
 
livestuffAuthor Commented:
I added Soap.HTTPUtil to uses clause and HTMLEscape is still unidentified
0
 
Marco GasiFreelancerCommented:
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
 
livestuffAuthor Commented:
ya, i figured and I am looking now
0
 
Marco GasiFreelancerCommented:
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
 
livestuffAuthor Commented:
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
 
Marco GasiFreelancerCommented:
Oooops: then use HtmlDecode (I'm not sure if it is in HTTPUtil or in HTTPApp)
0
 
livestuffAuthor Commented:
I have tried three different ones now and they all give me this error (see attached screenhot)
snap1.png
0
 
Marco GasiFreelancerCommented:
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
 
livestuffAuthor Commented:
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
 
livestuffAuthor Commented:
Turns out it was our XML! <smile>
0
 
Marco GasiFreelancerCommented:
Hi. Thanks for points. What your xml had wrong?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.