<detailedOutput>
<entity>
<entityLabel>Policy Holder 1</entityLabel>
<entityName>PHN.1</entityName>
<entityType>Policy Holder Name</entityType>
<fact>
<name>fired</name>
<nameContents>Y</nameContents>
<messageCode>M003</messageCode>
<reported>Y</reported>
<uniqueID>C_9_3_1</uniqueID>
<instanceID>1.1</instanceID>
</fact>
<fact>
<name>fired</name>
<nameContents>Y</nameContents>
<messageCode>M010</messageCode>
<reported>Y</reported>
<uniqueID>C_4_3_1</uniqueID>
<instanceID>1.1</instanceID>
</fact>
</entity>
<entity>
<entityLabel>property</entityLabel>
<entityName>PROP.1</entityName>
<entityType>Property</entityType>
<fact>
<name>propertyAddress</name>
<nameContents>1280 Somewhere Street, San Diego, WA 98312</nameContents>
</fact>
<fact>
<name>inqZipCode</name>
<nameContents>98312</nameContents>
</fact>
</detailedOutput>
I have an XML like this and I need to read all the values and store it in the table in SQL. I have following as my code:
for rec_facts in (
select EXTRACTVALUE(VALUE(TAB),'/entity/entityLabel/text()') as entity_label
,EXTRACTVALUE(VALUE(TAB),'/entity/entityName/text()') as entity_name
,EXTRACTVALUE(VALUE(TAB),'/entity/entityType/text()') as entity_type
FROM TABLE(XMLSEQUENCE(EXTRACT(SYS.XMLTYPE(v_xmltype),'/creditBureau/product/custom/riskVerificationPlatform/detailedOutput/entity'))) TAB
union all
select null, null, null from dual where not exists (select 1 from table(xmlsequence(extract(sys.xmltype(v_xmltype),'/creditBureau/product/custom/riskVerificationPlatform/detailedOutput/entity')))TAB1)
)
loop
for recout in (
select EXTRACTVALUE(VALUE(TAB2),'/fact/name/text()') as fact_name
,EXTRACTVALUE(VALUE(TAB2),'/fact/nameContents/text()') as fact_nameContents
,EXTRACTVALUE(VALUE(TAB2),'/fact/messageCode/text()') as fact_messageCode
,EXTRACTVALUE(VALUE(TAB2),'/fact/reported/text()') as fact_reported
,EXTRACTVALUE(VALUE(TAB2),'/fact/uniqueID/text()') as fact_uniqueID
,EXTRACTVALUE(VALUE(TAB2),'/fact/instanceID/text()') as fact_instanceID
TABLE(XMLSEQUENCE(EXTRACT(SYS.XMLTYPE(v_xmltype),'/creditBureau/product/custom/riskVerificationPlatform/message'))) TAB2
union all
select null, null, null, null, null, null from dual where not exists (select 1 from table(xmlsequence(extract(sys.xmltype(v_xmltype),'/creditBureau/product/custom/riskVerificationPlatform/message')))TAB2)
)
loop
insert into TUNA_FACTS
(TUNA_FACTS
,TUNA_WEB_REQUEST_RESPONSE
,ENTITY_LABEL
,ENTITY_NAME
,ENTITY_TYPE
,FACT_NAME
,FACT_NAMECONTENTS
,FACT_MESSAGECODE
,FACT_REPORTED
,FACT_UNIQUEID
,FACT_INSTANCEID
,CREATED_ON
,CREATED_BY
,AUDIT_ID
)
values
(seq_TUNA_FACTS.nextval
,v_seq_TUNA_web_req_res
,rec_facts.entity_label
,rec_facts.entity_name
,rec_facts.entity_type
,recout.fact_name
,recout.fact_nameContents
,recout.fact_messageCode
,recout.fact_reported
,recout.fact_uniqueID
,recout.fact_instanceID
,sysdate
,'TUNA'
,user);
end loop;
end loop;
The code runs, but it is repeating each entityLabel, entityName and entityType for 4 times instead of only 2 times. For example the table should be like this:
entityLabel entityName entityType FactName
--------------- ---------- ------------------ ---------------
Policy Holder 1 PHN.1 Policy Holder Name fired
Policy Holder 1 PHN.1 Policy Holder Name fired
property PROP.1 Property PropertyAddress
property PROP.1 Property inqZipCode
instead, it it creating a table like:
entityLabel entityName entityType FactName
--------------- ---------- ------------------ ---------------
Policy Holder 1 PHN.1 Policy Holder Name fired
Policy Holder 1 PHN.1 Policy Holder Name fired
Policy Holder 1 PHN.1 Policy Holder Name PropertyAddress
Policy Holder 1 PHN.1 Policy Holder Name inqZipCode
property PROP.1 Property fired
property PROP.1 Property fired
property PROP.1 Property PropertyAddress
property PROP.1 Property inqZipCode
I am sure it is a looping issue but I cannot figure out. Any help would be appreciated!