CODE - Tree Traversal

Hi Guys,

Need some help/guidance here, where my requirement is

  1. Source will send the XML data.

Example is:

<?xml version="1.0" encoding="UTF-8"?> 0001 Parent 0002 Parent 003 0001 Child 004 0001 Child 055 003 Child 066 003 Child 777 055 Child 008 0002 Child 099 004 Child 123 0002 Child
  1. In the above XML, contains 3 tags that are ID, RelID and Category.
  2. The one which does have have RelID, that becomes parent and the one that has some value becomes the child
    Example: In the above example:
    0001 is the parent

and

003 0001 Child

003, is the child for parent 0001.

Requirement is to convert the xml data to the following structure as attached in the screenshot. Any suggestion/code on how to proceed further with the logic here?

In the attached structure, we need to append the children to the respective parents in a hierarchy manner
Structure.PNG

I will give a try!

In your sample, you have two parents (RelID is empty), then?

Question to you - are you converting the IData (the one in the screen shot) back to xml, if so what is the expected final xml structure? and what is next step in once you get the desired IData structure?

The approach I think is by flow or using the XSL mapping, but need to understand the data structure that comes and what is expected out of it.

Hi Manju,

As per the SS the final xml structure should looks like deep recursive of Components/Component this correct? →

– Parent
<Component) - DocList - Deep recursive Loop
-



So now as per your source XMLdata map to the Target XMLdata and give your expected outcome instead of saying parent/child relation ship with the tags you narrated above:

HTH,
RMG

Hi Mahesh/rmg
Thanks a lot for the quick reply,

Here

  1. If the RelatedID (RID) is empty, which means its the parent and we can have any number of parents.

  2. As rmg said, its a deep recursive, that is any number of parents can have any number of children and any number of children can have any number of sub-children and so on.

  3. First we get XML data and then “apply the logic” (depends on each developer) and finally convert the XML data to the target structure, as shown in the screenshot attached.

  4. I have also attached the target structure one more time.

Problem here is: we tried all possible ways of dealing with ideas/code, but we are unable to end up to map to target structure, as we are not able to find how to insert into a document i.e child and sub child model.

Any small demo code will help a lot and unblock our development please.



Hi All,

We have written an XSLT Code and while parsing XML we are getting the below error. Any suggestion here please?

Hope this helps, managed to populate the IData structure (if this is the expected) for the below sample (one parent with multiple childrens), share me the xml sample with multiple parents and childrens, how each parent is linked with their respective childrens.

Feel free to contact me on email and I can be of some help if I can.

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> 
<OrderItems> 
	<OrderItem> 
		<ID>0001</ID> 
		<RelID></RelID> 
		<Category>Parent</Category> 
	</OrderItem> 
	<OrderItem> 
		<ID>0002</ID> 
		<RelID>0001</RelID> 
		<Category>Child</Category> 
	</OrderItem> 
	<OrderItem> 
		<ID>003</ID> 
		<RelID>0002</RelID> 
		<Category>Child</Category> 
	</OrderItem> 
	<OrderItem> 
		<ID>004</ID> 
		<RelID>0003</RelID> 
		<Category>Child</Category> 
	</OrderItem> 
	<OrderItem> 
		<ID>005</ID> 
		<RelID>004</RelID> 
		<Category>Child</Category> 
	</OrderItem> 
	<OrderItem> 
		<ID>006</ID> 
		<RelID>005</RelID> 
		<Category>Child</Category> 
	</OrderItem> 
	<OrderItem> 
		<ID>007</ID> 
		<RelID>006</RelID> 
		<Category>Child</Category> 
	</OrderItem> 
	<OrderItem> 
		<ID>008</ID> 
		<RelID>007</RelID> 
		<Category>Child</Category> 
	</OrderItem> 
	<OrderItem> 
		<ID>009</ID> 
		<RelID>008</RelID> 
		<Category>Child</Category> 
	</OrderItem> 
	<OrderItem> 
		<ID>010</ID> 
		<RelID>009</RelID> 
		<Category>Child</Category> 
	</OrderItem> 
</OrderItems> 

Hi,

I understand this is a complex XML with parent/child (recursive scenario) is your target system is expecting this way of format?
But I believe you are in a right path using XSLT Transformation handling of this kind of xml complexity.

Will also take a look on the error apart from Mahesh :slight_smile:

HTH,
RMG