But when I try to make a query to get CostCenter value from the XML, I get errors.
Here is the query I tried:
SELECT
(xmltype(iden.attributes).extract(‘Attributes/Map/entry[@key="CostCenter"]/@value’).getStringVal()) AS CostCenter
FROM spt_identity iden WHERE iden.name=‘211678016’;
Here is the error:
ORA-31011: XML parsing failed
ORA-19202: Error occurred in XML processing
LPX-00601: Invalid token in: ‘Attributes/Map/entry[@key="CostCenter"]/@value’
31011. 00000 - “XML parsing failed”
*Cause: XML parser returned an error while trying to parse the document.
*Action: Check if the document to be parsed is valid.
Please help me to create the right queryPreformatted text``Preformatted text
Could you check if the characters used are standard ASCII characters, especially the quotes (',") used.
Try if this works
SELECT
xmltype(iden.attributes).extract('/Attributes/Map/entry[@key="CostCenter"]/@value').getStringVal() AS CostCenter
FROM
spt_identity iden
WHERE
iden.name='211678016';
Now I need to find all identities where the identites do not have specific metadata in it.
There are three types of employees.
Some may have metadata like below: <AttributeMetaData attribute="email" source="MailTFAYD:mail"/>
Some may have metadata like below: <AttributeMetaData attribute="email" source="Application_HR:C001_EMAIL"/>
Others may have metadata like below:
Now I need to find all those employees who do not have metada <AttributeMetaData attribute="email" source="MailTFAYD:mail"/>
I tried query like below:
SELECT iden.name
FROM
spt_identity iden
WHERE
xmltype(iden.ATTRIBUTE_META_DATA).extract(‘/List/AttributeMetaData[@attribute=“email”]/@source’).getStringVal() != null AND
xmltype(iden.ATTRIBUTE_META_DATA).extract(‘/List/AttributeMetaData[@attribute=“email”]/@source’).getStringVal() !=‘MailTFAYD:mail’;
Getting below error:
ORA-31011: XML parsing failed
ORA-19202: Error occurred in XML processing
LPX-00007: unexpected end-of-file encountered
ORA-06512: at “SYS.XMLTYPE”, line 272
ORA-06512: at line 1
31011. 00000 - “XML parsing failed”
*Cause: XML parser returned an error while trying to parse the document.
*Action: Check if the document to be parsed is valid.
The error is due to non-standard ASCII characters in the query. Please try the following query.
If my response resolves your issue, please mark my response as the solution.
SELECT iden.name
FROM
spt_identity iden
WHERE
xmltype(iden.ATTRIBUTE_META_DATA).extract('/List/AttributeMetaData[@attribute="email"]/@source').getStringVal() IS NOT NULL AND
xmltype(iden.ATTRIBUTE_META_DATA).extract('/List/AttributeMetaData[@attribute="email"]/@source').getStringVal() != 'MailTFAYD:mail';
Getting same error:
ORA-31011: XML parsing failed
ORA-19202: Error occurred in XML processing
LPX-00007: unexpected end-of-file encountered
ORA-06512: at “SYS.XMLTYPE”, line 272
ORA-06512: at line 1
31011. 00000 - “XML parsing failed”
*Cause: XML parser returned an error while trying to parse the document.
*Action: Check if the document to be parsed is valid.
SELECT iden.name
FROM spt_identity iden
WHERE NOT XMLExists(
'/List/AttributeMetaData[@attribute="email" and @source="MailTFAYD:mail"]'
PASSING XMLType(iden.ATTRIBUTE_META_DATA)
);
When I try to get single record like using below query:
SELECT ATTRIBUTE_META_DATA FROM spt_identity where name=‘202678012’;
I am able to see the record like below:
And when I try to find that email metadata attribute using below query:
SELECT xmltype(iden.ATTRIBUTE_META_DATA).extract(‘/List/AttributeMetaData[@attribute=“email”]/@source’).getStringVal() as EM
FROM spt_identity iden
WHERE name=‘202678012’;
Then also I am able to get the answer as below:
Application_HR:C001_EMAIL
But when I try above query without where clause the getting the same error again.
Query:
SELECT xmltype(iden.ATTRIBUTE_META_DATA).extract(‘/List/AttributeMetaData[@attribute=“email”]/@source’).getStringVal() as EM
FROM spt_identity iden;
Result:
Same error.
Looks like when I try to parse xml for multiple records then the parsing is failing. Not sure how to resolve.
Thank you so much Sreeram for helping me out.
Looks like you gave correct query. The parsing error was due to junk data in that column.
When I added null checks in the query it worked fine. Here is the updated query I used:
SELECT NAME, EMAIL FROM (
SELECT
NAME, xmltype(ATTRIBUTE_META_DATA).extract(‘/List/AttributeMetaData[@attribute=“email”]/@source’).getStringVal() AS EMAIL
FROM spt_identity
WHERE
NOT XMLEXISTS(‘/List/AttributeMetaData[@attribute=“email” and @source=“MailTFAYD:mail”]’ PASSING XMLType(ATTRIBUTE_META_DATA))
AND ATTRIBUTE_META_DATA IS NOT NULL
AND length(ATTRIBUTE_META_DATA) > 1
)
WHERE EMAIL IS NOT NULL
AND length(EMAIL) > 1;
This query is returning output like below: