Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Selecting a particular Node value in a XML Document

Posted on 2013-11-22
17
Medium Priority
?
3,340 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
17 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 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