Author |
Message
|
Harman |
Posted: Thu Oct 29, 2020 6:07 am Post subject: Accessing XML tags and their values |
|
|
Newbie
Joined: 22 Apr 2020 Posts: 9
|
Hi , I have XML structure like this :
Code: |
<parent>
<first>
<second>
<third attr="name" attr1="test1" />
<third attr="age" attr1="25" />
</second>
<second w="0">
<third attr="name" attr1="test2" />
<third attr="age" attr1="26" />
</second>
</first>
</parent> |
I want to access the attr1 that is test1 using attr that is name , is it possible at all ? |
|
Back to top |
|
 |
Vitor |
Posted: Thu Oct 29, 2020 8:33 am Post subject: Re: Accessing XML tags and their values |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
Harman wrote: |
I want to access the attr1 that is test1 using attr that is name , is it possible at all ? |
Yes. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
Harman |
Posted: Thu Oct 29, 2020 9:09 am Post subject: |
|
|
Newbie
Joined: 22 Apr 2020 Posts: 9
|
@Vitor I tried playing around with XML.Element and XML.Attr but I dont think I am doing it in a correct way.
Something like
DECLARE test_var CHAR FIELDVALUE(InputRoot.XMLNSC.parent.first.second[1].(XML.Element)f)
Could you please provide your inputs on this? |
|
Back to top |
|
 |
Vitor |
Posted: Thu Oct 29, 2020 10:48 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
Wouldn't it be more like:
Code: |
DECLARE test_var CHAR FIELDVALUE(InputRoot.XMLNSC.parent.first.second[1].third[1].(XML.Attr)attr)
|
_________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
Harman |
Posted: Thu Oct 29, 2020 7:14 pm Post subject: |
|
|
Newbie
Joined: 22 Apr 2020 Posts: 9
|
@Vitor , ok . So the <third> is kind of array again which needs to be handled in a loop like <second> . Got it . Thank you ! |
|
Back to top |
|
 |
Vitor |
Posted: Fri Oct 30, 2020 4:36 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
Harman wrote: |
@Vitor , ok . So the <third> is kind of array again which needs to be handled in a loop like <second> . Got it . |
No you have not.
In your example, <first> is a complex XML type, made up of an iteration of the complex type <second>. That, in turn, is made up of an iteration of the simple type <third>, which also has attributes.
This is basic XML and nothing to do with IIB/ACE. Ask Google for a good XML tutorial and read it until you understand you can't just ignore an XML element (like <third>).
They're not an array, they are just an iterative list. ESQL allows you to use array indexes to reference them but on an XML document of any size this is hideously inefficient. ESQL does not maintain a pointer to the list so when you access element n, the software starts with first element and counts through the document n times. This is fine for small documents like your example, but with an iteration of 100 elements you'll really see that run time extend. Much better is to define a user pointer in code and move that through the list yourself. As I said, not needed in your small example but it's a good idea to get into the habit.
Because this behavior can cause a "gotcha". If you access elements by index (as you are) and delete an element (good practice in large documents, saves memory), the count changes.
Consider this example:
You access element 30 in the list. The ESQL element[30] causes the broker to count until it finds the 30th element. You process this and remove it from the tree. You then access element[31] in your code (probably by incrementing a loop variable). The broker will count until it reaches 31 taking no account of the element which is now deleted. So the element you find at position 31 is the element which, prior to deletion, was element 32 and an element is never processed. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
timber |
Posted: Sat Oct 31, 2020 2:27 pm Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
@Vitor: I think Harman probably needs a SELECT statement, or else at last one loop. But I cannot be sure...
@Harman : I do not understand exactly what you need to do. Please can you include some more examples. |
|
Back to top |
|
 |
rekarm01 |
Posted: Tue Nov 10, 2020 10:24 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 1415
|
Harman wrote: |
@Vitor I tried playing around with XML.Element and XML.Attr but I dont think I am doing it in a correct way. |
It's not always necessary to use parser constants to qualify the type of field references. But if the code does use them, they should match the parser. Use XMLNSC parser constants with the XMLNSC parser, such as (XMLNSC.Field) or (XMLNSC.Attribute), instead of XML parser constants. |
|
Back to top |
|
 |
|