问题
I am trying to parse an XML recursively to create JSON array using dataweave 2 but I am not able to do so.
My Input XML is given below -
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId>{8736346D-A766-4BE1-8592-C263A4CC2AB5}</MessageId>
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/BomBillsofMaterialsService/read</Action>
</Header>
<Body>
<MessageParts xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<BillsOfMaterials xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/BillsOfMaterials">
<DocPurpose>Original</DocPurpose>
<SenderId>121</SenderId>
<BOMVersion class="entity">
<_DocumentHash>5c32768c0e32d542255d67340fda7a9d</_DocumentHash>
<Active>Yes</Active>
<Approved>Yes</Approved>
<Approver>NoUser</Approver>
<BOMId>BOM0012610</BOMId>
<Construction>No</Construction>
<InventDimId>IDM0000001</InventDimId>
<ItemId>9641716</ItemId>
<ItemIdCommercial/>
<Name>Prepress Bundle - Folding Carton</Name>
<QBE_AddedByContainer>Yes</QBE_AddedByContainer>
<RecId>5637161354</RecId>
<RecVersion>1034011536</RecVersion>
<BOMTable class="entity">
<Approved>Yes</Approved>
<Approver>NoUser</Approver>
<BOMId>BOM0012610</BOMId>
<CheckBOM>Yes</CheckBOM>
<Name>Prepress Bundle - Folding Carton</Name>
<RecId>5637160605</RecId>
<RecVersion>450200996</RecVersion>
<SiteId>2</SiteId>
<BOM class="entity">
<BOMConsump>Variable</BOMConsump>
<BOMId>BOM0012610</BOMId>
<BOMQty>1.0000</BOMQty>
<BOMQtySerie>1</BOMQtySerie>
<BOMType>Item</BOMType>
<Calculation>Yes</Calculation>
<EndSchedConsump>No</EndSchedConsump>
<Formula>Formula0</Formula>
<InventDimId>IDM0000031</InventDimId>
<ItemId>96AR169X</ItemId>
<LineNum>1.0000000000</LineNum>
<ProdFlushingPrincip>Blank</ProdFlushingPrincip>
<ProjSetSubProdToConsumed>No</ProjSetSubProdToConsumed>
<RecId>5637300477</RecId>
<RecVersion>1</RecVersion>
<RoundUp>No</RoundUp>
<UnitId>PCS</UnitId>
<WrkCtrConsumption>false</WrkCtrConsumption>
<InventDimBOM class="entity">
<InventDimId>IDM0000031</InventDimId>
<InventLocationId>51ST</InventLocationId>
<InventSiteId>2</InventSiteId>
<RecId>5637213319</RecId>
<RecVersion>1</RecVersion>
</InventDimBOM>
<BOMTable>
<BOMId>BOM011986</BOMId>
<Name/>
<Approved>Yes</Approved>
<BOM>
<LineNum>1.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>96AR060W</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>111111111</RecId>
</BOM>
<BOM>
<LineNum>2.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>96AR158</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>222222</RecId>
</BOM>
<BOM>
<LineNum>3.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>96AR582O</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>333333333</RecId>
<BOMTable>
<BOMId>BOM0013773</BOMId>
<Name>ArtiosCAD 14 A6 Software box (Standard Edition)</Name>
<Approved>Yes</Approved>
<BOM>
<LineNum>1000.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>G211249</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>4444444</RecId>
</BOM>
<BOM>
<LineNum>1006.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>G25583954_46</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>55555555</RecId>
</BOM>
</BOMTable>
<BOMTable>
<BOMId>BOM0013773</BOMId>
<Name>ArtiosCAD 14 A6 Software box (Standard Edition)</Name>
<Approved>Yes</Approved>
<RecId>333333333</RecId>
<BOM>
<LineNum>1000.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>G211249</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>66666666</RecId>
</BOM>
<BOM>
<LineNum>1006.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>G25583954_46</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>77777777777</RecId>
</BOM>
</BOMTable>
</BOM>
</BOMTable>
</BOM>
<BOM class="entity">
<BOMConsump>Variable</BOMConsump>
<BOMId>BOM0012610</BOMId>
<BOMQty>1.0000</BOMQty>
<BOMQtySerie>1</BOMQtySerie>
<BOMType>Item</BOMType>
<Calculation>Yes</Calculation>
<EndSchedConsump>No</EndSchedConsump>
<Formula>Formula0</Formula>
<InventDimId>IDM0000002</InventDimId>
<ItemId>9646095W</ItemId>
<LineNum>11.0000000000</LineNum>
<ProdFlushingPrincip>Blank</ProdFlushingPrincip>
<ProjSetSubProdToConsumed>No</ProjSetSubProdToConsumed>
<RecId>5637300487</RecId>
<RecVersion>1</RecVersion>
<RoundUp>No</RoundUp>
<UnitId>PCS</UnitId>
<WrkCtrConsumption>false</WrkCtrConsumption>
<InventDimBOM class="entity">
<InventDimId>IDM0000002</InventDimId>
<InventLocationId>NOLOC</InventLocationId>
<InventSiteId>2</InventSiteId>
<RecId>5637213290</RecId>
<RecVersion>1</RecVersion>
</InventDimBOM>
</BOM>
<BOM class="entity">
<BOMConsump>Variable</BOMConsump>
<BOMId>BOM0012610</BOMId>
<BOMQty>1.0000</BOMQty>
<BOMQtySerie>1</BOMQtySerie>
<BOMType>Item</BOMType>
<Calculation>Yes</Calculation>
<EndSchedConsump>No</EndSchedConsump>
<Formula>Formula0</Formula>
<InventDimId>IDM0000002</InventDimId>
<ItemId>9648376X</ItemId>
<LineNum>12.0000000000</LineNum>
<ProdFlushingPrincip>Blank</ProdFlushingPrincip>
<ProjSetSubProdToConsumed>No</ProjSetSubProdToConsumed>
<RecId>5637300488</RecId>
<RecVersion>1</RecVersion>
<RoundUp>No</RoundUp>
<UnitId>PCS</UnitId>
<WrkCtrConsumption>false</WrkCtrConsumption>
<InventDimBOM class="entity">
<InventDimId>IDM0000002</InventDimId>
<InventLocationId>NOLOC</InventLocationId>
<InventSiteId>2</InventSiteId>
<RecId>5637213290</RecId>
<RecVersion>1</RecVersion>
</InventDimBOM>
<BOMTable>
<BOMId>BOM011638</BOMId>
<Name/>
<Approved>Yes</Approved>
<BOM>
<LineNum>1.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>9645340X</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>888888888</RecId>
</BOM>
<BOM>
<LineNum>2.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>9644384Y</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>9999999999</RecId>
</BOM>
</BOMTable>
</BOM>
<BOM class="entity">
<BOMConsump>Variable</BOMConsump>
<BOMId>BOM0012610</BOMId>
<BOMQty>1.0000</BOMQty>
<BOMQtySerie>1</BOMQtySerie>
<BOMType>Item</BOMType>
<Calculation>Yes</Calculation>
<EndSchedConsump>No</EndSchedConsump>
<Formula>Formula0</Formula>
<InventDimId>IDM0000003</InventDimId>
<ItemId>9643908X</ItemId>
<LineNum>13.0000000000</LineNum>
<ProdFlushingPrincip>Blank</ProdFlushingPrincip>
<ProjSetSubProdToConsumed>No</ProjSetSubProdToConsumed>
<RecId>5637300489</RecId>
<RecVersion>1</RecVersion>
<RoundUp>No</RoundUp>
<UnitId>PCS</UnitId>
<WrkCtrConsumption>false</WrkCtrConsumption>
<InventDimBOM class="entity">
<InventDimId>IDM0000003</InventDimId>
<InventLocationId>01</InventLocationId>
<InventSiteId>2</InventSiteId>
<RecId>5637213291</RecId>
<RecVersion>1</RecVersion>
</InventDimBOM>
<BOMTable>
<BOMId>BOM011299</BOMId>
<Name/>
<Approved>Yes</Approved>
<RecId>555555555</RecId>
<BOM>
<LineNum>1.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>9642737Y</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>101010101</RecId>
</BOM>
<BOM>
<LineNum>3.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>9642736Y</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>1112112112</RecId>
</BOM>
</BOMTable>
</BOM>
<BOM class="entity">
<BOMConsump>Variable</BOMConsump>
<BOMId>BOM0012610</BOMId>
<BOMQty>1.0000</BOMQty>
<BOMQtySerie>1</BOMQtySerie>
<BOMType>Item</BOMType>
<Calculation>Yes</Calculation>
<EndSchedConsump>No</EndSchedConsump>
<Formula>Formula0</Formula>
<InventDimId>IDM0000003</InventDimId>
<ItemId>9642837X</ItemId>
<LineNum>14.0000000000</LineNum>
<ProdFlushingPrincip>Blank</ProdFlushingPrincip>
<ProjSetSubProdToConsumed>No</ProjSetSubProdToConsumed>
<RecId>5637300490</RecId>
<RecVersion>1</RecVersion>
<RoundUp>No</RoundUp>
<UnitId>PCS</UnitId>
<WrkCtrConsumption>false</WrkCtrConsumption>
<InventDimBOM class="entity">
<InventDimId>IDM0000003</InventDimId>
<InventLocationId>01</InventLocationId>
<InventSiteId>2</InventSiteId>
<RecId>5637213291</RecId>
<RecVersion>1</RecVersion>
</InventDimBOM>
<BOMTable>
<BOMId>BOM011245</BOMId>
<Name/>
<Approved>Yes</Approved>
<BOM>
<LineNum>1.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>9644384Y</ItemId>
<BOMQty>-1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>13131313</RecId>
</BOM>
</BOMTable>
</BOM>
</BOMTable>
<InventDim class="entity">
<InventDimId>IDM0000001</InventDimId>
<InventSiteId>2</InventSiteId>
<RecId>5637199988</RecId>
<RecVersion>1</RecVersion>
</InventDim>
</BOMVersion>
</BillsOfMaterials>
</MessageParts>
</Body>
</Envelope>
I want to create JSON array of all the BOMs (a flat structure with no hierarchy) as shown below -
Output -
[
{
"id": "5637300477",
"productId": "96AR169X",
"parentId": null
},
{
"id": "111111111",
"productId": "96AR060W",
"parentId" : "BOM011986"
},
{
"id": "222222",
"productId": "96AR158",
"parentId" : "BOM011986"
},
{
"id": "333333333",
"productId": "96AR582O",
"parentId": "BOM011986"
},
{
"id": "4444444",
"productId": "G211249",
"parentId" : "BOM0013773"
},
{
"id": "55555555",
"productId": "G25583954_46",
"parentId" : "BOM0013773"
},
{
"id": "66666666",
"productId": "BOM0013773",
"parentId" : "BOM0013773"
},
{
"id": "77777777777",
"productId": "G25583954_46",
"parentId" : "BOM0013773"
},
{
"id": "5637300487",
"productId": "9646095W",
"parentId" : null
},
{
"id": "5637300488",
"productId": "9648376X",
"parentId" : null
},
.
]
I not able to figure it out the use of recursion in this case. I am quite new to Dataweave language as it is functional language. I am used to Java language.
Thanks in advance for help !!!
回答1:
You can make use of descendant data selector (using ..) to retrieve all BOMTable from your xml document and thus there is no need to recursively navigate through your document. Then using map/flatMap to output the necessary fields.
Please see below dataweave:
%dw 2.0
output application/json
---
payload..*BOMTable flatMap (bomTable) -> bomTable.*BOM map (bomValue) ->
{
id: bomValue.RecId,
productId: bomValue.ItemId,
parentId: bomTable.RecId
}
Output will be as follows:
[
{
"id": "5637300477",
"productId": "96AR169X",
"parentId": "5637160605"
},
{
"id": "5637300487",
"productId": "9646095W",
"parentId": "5637160605"
},
{
"id": "5637300488",
"productId": "9648376X",
"parentId": "5637160605"
},
{
"id": "5637300489",
"productId": "9643908X",
"parentId": "5637160605"
},
{
"id": "5637300490",
"productId": "9642837X",
"parentId": "5637160605"
},
{
"id": "111111111",
"productId": "96AR060W",
"parentId": null
},
{
"id": "222222",
"productId": "96AR158",
"parentId": null
},
{
"id": "333333333",
"productId": "96AR582O",
"parentId": null
},
{
"id": "4444444",
"productId": "G211249",
"parentId": null
},
{
"id": "55555555",
"productId": "G25583954_46",
"parentId": null
},
{
"id": "66666666",
"productId": "G211249",
"parentId": "333333333"
},
{
"id": "77777777777",
"productId": "G25583954_46",
"parentId": "333333333"
},
{
"id": "888888888",
"productId": "9645340X",
"parentId": null
},
{
"id": "9999999999",
"productId": "9644384Y",
"parentId": null
},
{
"id": "101010101",
"productId": "9642737Y",
"parentId": "555555555"
},
{
"id": "1112112112",
"productId": "9642736Y",
"parentId": "555555555"
},
{
"id": "13131313",
"productId": "9644384Y",
"parentId": null
}
]
Note that the output is still different from the expected output that you posted which I think is incorrect based on the mapping rules you posted in one of your comments.
回答2:
Can you please try the below dwl
%dw 2.0
output application/json
var payload = read('<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId>{8736346D-A766-4BE1-8592-C263A4CC2AB5}</MessageId>
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/BomBillsofMaterialsService/read</Action>
</Header>
<Body>
<MessageParts xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<BillsOfMaterials xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/BillsOfMaterials">
<DocPurpose>Original</DocPurpose>
<SenderId>121</SenderId>
<BOMVersion class="entity">
<_DocumentHash>5c32768c0e32d542255d67340fda7a9d</_DocumentHash>
<Active>Yes</Active>
<Approved>Yes</Approved>
<Approver>NoUser</Approver>
<BOMId>BOM0012610</BOMId>
<Construction>No</Construction>
<InventDimId>IDM0000001</InventDimId>
<ItemId>9641716</ItemId>
<ItemIdCommercial/>
<Name>Prepress Bundle - Folding Carton</Name>
<QBE_AddedByContainer>Yes</QBE_AddedByContainer>
<RecId>5637161354</RecId>
<RecVersion>1034011536</RecVersion>
<BOMTable class="entity">
<Approved>Yes</Approved>
<Approver>NoUser</Approver>
<BOMId>BOM0012610</BOMId>
<CheckBOM>Yes</CheckBOM>
<Name>Prepress Bundle - Folding Carton</Name>
<RecId>5637160605</RecId>
<RecVersion>450200996</RecVersion>
<SiteId>2</SiteId>
<BOM class="entity">
<BOMConsump>Variable</BOMConsump>
<BOMId>BOM0012610</BOMId>
<BOMQty>1.0000</BOMQty>
<BOMQtySerie>1</BOMQtySerie>
<BOMType>Item</BOMType>
<Calculation>Yes</Calculation>
<EndSchedConsump>No</EndSchedConsump>
<Formula>Formula0</Formula>
<InventDimId>IDM0000031</InventDimId>
<ItemId>96AR169X</ItemId>
<LineNum>1.0000000000</LineNum>
<ProdFlushingPrincip>Blank</ProdFlushingPrincip>
<ProjSetSubProdToConsumed>No</ProjSetSubProdToConsumed>
<RecId>5637300477</RecId>
<RecVersion>1</RecVersion>
<RoundUp>No</RoundUp>
<UnitId>PCS</UnitId>
<WrkCtrConsumption>false</WrkCtrConsumption>
<InventDimBOM class="entity">
<InventDimId>IDM0000031</InventDimId>
<InventLocationId>51ST</InventLocationId>
<InventSiteId>2</InventSiteId>
<RecId>5637213319</RecId>
<RecVersion>1</RecVersion>
</InventDimBOM>
<BOMTable>
<BOMId>BOM011986</BOMId>
<Name/>
<Approved>Yes</Approved>
<BOM>
<LineNum>1.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>96AR060W</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>111111111</RecId>
</BOM>
<BOM>
<LineNum>2.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>96AR158</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>222222</RecId>
</BOM>
<BOM>
<LineNum>3.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>96AR582O</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>333333333</RecId>
<BOMTable>
<BOMId>BOM0013773</BOMId>
<Name>ArtiosCAD 14 A6 Software box (Standard Edition)</Name>
<Approved>Yes</Approved>
<BOM>
<LineNum>1000.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>G211249</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>4444444</RecId>
</BOM>
<BOM>
<LineNum>1006.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>G25583954_46</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>55555555</RecId>
</BOM>
</BOMTable>
<BOMTable>
<BOMId>BOM0013773</BOMId>
<Name>ArtiosCAD 14 A6 Software box (Standard Edition)</Name>
<Approved>Yes</Approved>
<RecId>333333333</RecId>
<BOM>
<LineNum>1000.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>G211249</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>66666666</RecId>
</BOM>
<BOM>
<LineNum>1006.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>G25583954_46</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>77777777777</RecId>
</BOM>
</BOMTable>
</BOM>
</BOMTable>
</BOM>
<BOM class="entity">
<BOMConsump>Variable</BOMConsump>
<BOMId>BOM0012610</BOMId>
<BOMQty>1.0000</BOMQty>
<BOMQtySerie>1</BOMQtySerie>
<BOMType>Item</BOMType>
<Calculation>Yes</Calculation>
<EndSchedConsump>No</EndSchedConsump>
<Formula>Formula0</Formula>
<InventDimId>IDM0000002</InventDimId>
<ItemId>9646095W</ItemId>
<LineNum>11.0000000000</LineNum>
<ProdFlushingPrincip>Blank</ProdFlushingPrincip>
<ProjSetSubProdToConsumed>No</ProjSetSubProdToConsumed>
<RecId>5637300487</RecId>
<RecVersion>1</RecVersion>
<RoundUp>No</RoundUp>
<UnitId>PCS</UnitId>
<WrkCtrConsumption>false</WrkCtrConsumption>
<InventDimBOM class="entity">
<InventDimId>IDM0000002</InventDimId>
<InventLocationId>NOLOC</InventLocationId>
<InventSiteId>2</InventSiteId>
<RecId>5637213290</RecId>
<RecVersion>1</RecVersion>
</InventDimBOM>
</BOM>
<BOM class="entity">
<BOMConsump>Variable</BOMConsump>
<BOMId>BOM0012610</BOMId>
<BOMQty>1.0000</BOMQty>
<BOMQtySerie>1</BOMQtySerie>
<BOMType>Item</BOMType>
<Calculation>Yes</Calculation>
<EndSchedConsump>No</EndSchedConsump>
<Formula>Formula0</Formula>
<InventDimId>IDM0000002</InventDimId>
<ItemId>9648376X</ItemId>
<LineNum>12.0000000000</LineNum>
<ProdFlushingPrincip>Blank</ProdFlushingPrincip>
<ProjSetSubProdToConsumed>No</ProjSetSubProdToConsumed>
<RecId>5637300488</RecId>
<RecVersion>1</RecVersion>
<RoundUp>No</RoundUp>
<UnitId>PCS</UnitId>
<WrkCtrConsumption>false</WrkCtrConsumption>
<InventDimBOM class="entity">
<InventDimId>IDM0000002</InventDimId>
<InventLocationId>NOLOC</InventLocationId>
<InventSiteId>2</InventSiteId>
<RecId>5637213290</RecId>
<RecVersion>1</RecVersion>
</InventDimBOM>
<BOMTable>
<BOMId>BOM011638</BOMId>
<Name/>
<Approved>Yes</Approved>
<BOM>
<LineNum>1.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>9645340X</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>888888888</RecId>
</BOM>
<BOM>
<LineNum>2.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>9644384Y</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>9999999999</RecId>
</BOM>
</BOMTable>
</BOM>
<BOM class="entity">
<BOMConsump>Variable</BOMConsump>
<BOMId>BOM0012610</BOMId>
<BOMQty>1.0000</BOMQty>
<BOMQtySerie>1</BOMQtySerie>
<BOMType>Item</BOMType>
<Calculation>Yes</Calculation>
<EndSchedConsump>No</EndSchedConsump>
<Formula>Formula0</Formula>
<InventDimId>IDM0000003</InventDimId>
<ItemId>9643908X</ItemId>
<LineNum>13.0000000000</LineNum>
<ProdFlushingPrincip>Blank</ProdFlushingPrincip>
<ProjSetSubProdToConsumed>No</ProjSetSubProdToConsumed>
<RecId>5637300489</RecId>
<RecVersion>1</RecVersion>
<RoundUp>No</RoundUp>
<UnitId>PCS</UnitId>
<WrkCtrConsumption>false</WrkCtrConsumption>
<InventDimBOM class="entity">
<InventDimId>IDM0000003</InventDimId>
<InventLocationId>01</InventLocationId>
<InventSiteId>2</InventSiteId>
<RecId>5637213291</RecId>
<RecVersion>1</RecVersion>
</InventDimBOM>
<BOMTable>
<BOMId>BOM011299</BOMId>
<Name/>
<Approved>Yes</Approved>
<RecId>555555555</RecId>
<BOM>
<LineNum>1.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>9642737Y</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>101010101</RecId>
</BOM>
<BOM>
<LineNum>3.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>9642736Y</ItemId>
<BOMQty>1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>1112112112</RecId>
</BOM>
</BOMTable>
</BOM>
<BOM class="entity">
<BOMConsump>Variable</BOMConsump>
<BOMId>BOM0012610</BOMId>
<BOMQty>1.0000</BOMQty>
<BOMQtySerie>1</BOMQtySerie>
<BOMType>Item</BOMType>
<Calculation>Yes</Calculation>
<EndSchedConsump>No</EndSchedConsump>
<Formula>Formula0</Formula>
<InventDimId>IDM0000003</InventDimId>
<ItemId>9642837X</ItemId>
<LineNum>14.0000000000</LineNum>
<ProdFlushingPrincip>Blank</ProdFlushingPrincip>
<ProjSetSubProdToConsumed>No</ProjSetSubProdToConsumed>
<RecId>5637300490</RecId>
<RecVersion>1</RecVersion>
<RoundUp>No</RoundUp>
<UnitId>PCS</UnitId>
<WrkCtrConsumption>false</WrkCtrConsumption>
<InventDimBOM class="entity">
<InventDimId>IDM0000003</InventDimId>
<InventLocationId>01</InventLocationId>
<InventSiteId>2</InventSiteId>
<RecId>5637213291</RecId>
<RecVersion>1</RecVersion>
</InventDimBOM>
<BOMTable>
<BOMId>BOM011245</BOMId>
<Name/>
<Approved>Yes</Approved>
<BOM>
<LineNum>1.00</LineNum>
<BOMType>Item</BOMType>
<ItemId>9644384Y</ItemId>
<BOMQty>-1.00</BOMQty>
<OprNum>0</OprNum>
<FromDate>1900-01-01</FromDate>
<UnitId>PCS</UnitId>
<RecId>13131313</RecId>
</BOM>
</BOMTable>
</BOM>
</BOMTable>
<InventDim class="entity">
<InventDimId>IDM0000001</InventDimId>
<InventSiteId>2</InventSiteId>
<RecId>5637199988</RecId>
<RecVersion>1</RecVersion>
</InventDim>
</BOMVersion>
</BillsOfMaterials>
</MessageParts>
</Body>
</Envelope>',"application/xml")
---
flatten((payload.Envelope.Body.MessageParts.BillsOfMaterials.BOMVersion..*BOMTable map
{
BOMTable : $.*BOM map (val) -> {
"id": val.RecId,
"productId": val.ItemId,
parentId : $.RecId
}
}).*BOMTable)
Output is
[
{
"id": "5637300477",
"productId": "96AR169X",
"parentId": "5637160605"
},
{
"id": "5637300487",
"productId": "9646095W",
"parentId": "5637160605"
},
{
"id": "5637300488",
"productId": "9648376X",
"parentId": "5637160605"
},
{
"id": "5637300489",
"productId": "9643908X",
"parentId": "5637160605"
},
{
"id": "5637300490",
"productId": "9642837X",
"parentId": "5637160605"
},
{
"id": "111111111",
"productId": "96AR060W",
"parentId": null
},
{
"id": "222222",
"productId": "96AR158",
"parentId": null
},
{
"id": "333333333",
"productId": "96AR582O",
"parentId": null
},
{
"id": "4444444",
"productId": "G211249",
"parentId": null
},
{
"id": "55555555",
"productId": "G25583954_46",
"parentId": null
},
{
"id": "66666666",
"productId": "G211249",
"parentId": "333333333"
},
{
"id": "77777777777",
"productId": "G25583954_46",
"parentId": "333333333"
},
{
"id": "888888888",
"productId": "9645340X",
"parentId": null
},
{
"id": "9999999999",
"productId": "9644384Y",
"parentId": null
},
{
"id": "101010101",
"productId": "9642737Y",
"parentId": "555555555"
},
{
"id": "1112112112",
"productId": "9642736Y",
"parentId": "555555555"
},
{
"id": "13131313",
"productId": "9644384Y",
"parentId": null
}
]
来源:https://stackoverflow.com/questions/63994711/recursively-parse-xml-to-create-json-array-using-dataweave-2