Home » Developer & Programmer » JDeveloper, Java & XML » How to fetch tag data from XML string?
How to fetch tag data from XML string? [message #162238] Thu, 09 March 2006 05:44 Go to next message
b_chugh
Messages: 68
Registered: August 2005
Location: delhi
Member
hello
Please if any one can help. I am trying to fetch data through the following code.


p := xmlparser.newParser;
xmlparser.setValidationMode(p, FALSE);
xmlparser.parseclob(p, inout_XML);
docRequest := xmlparser.getDocument(p);

lvsActvMsg := 'Parsing parameters';
lvsEntityTypCd := xslprocessor.valueOf(xmldom.makeNode(docRequest),'//PARAMETERS/PARAMETER[@NAME="ENTITYTYPCD"]');

lvnEntityNbr := xslprocessor.valueOf(xmldom.makeNode(docRequest),'//PARAMETERS/PARAMETER[@NAME="ENTITYNBR"]');

lvsUserFieldCd := xslprocessor.valueOf(xmldom.makeNode(docRequest),'//PARAMETERS/PARAMETER[@NAME="USERFIELDCD"]');


and I am passing this string into the inout_XML
lvsXML := '<PROCREQ PROC=' || '"' || '2010' || '"' || '><ACCESS_NBR>145660</ACCESS_NBR><PIN></PIN><SOURCE>VRU</SOURCE><UC>1</UC><ENTITYTYPCD>PERS</ENTITYTYPCD><ENTITYNBR>4294 </ENTITYNBR><USERFIELDCD></USERFIELDCD>PMTU<USERFIELDCD></USERFIELDCD></PROCREQ>';


Re: How to fetch tag data from XML string? [message #162599 is a reply to message #162238] Sat, 11 March 2006 16:16 Go to previous messageGo to next message
mchadder
Messages: 224
Registered: May 2005
Location: UK
Senior Member
Hello there.

Not sure what's happening with the XMLPARSER approach you're using, but there are more than one way to skin a cat in Oracle, especially for processing XML, so here's an alternative using the SQL XPath extract mechanisms :
SQL> DECLARE
  2    x  XMLTYPE := XMLTYPE('<PROCREQ PROC="2010">
  3                             <ACCESS_NBR>145660</ACCESS_NBR>
  4                             <PIN></PIN>
  5                             <SOURCE>VRU</SOURCE>
  6                             <UC>1</UC>
  7                             <ENTITYTYPCD>PERS</ENTITYTYPCD>
  8                             <ENTITYNBR>4294 </ENTITYNBR>
  9                             <USERFIELDCD></USERFIELDCD>
 10                             PMTU
 11                             <USERFIELDCD></USERFIELDCD>
 12                           </PROCREQ>');
 13  BEGIN
 14    FOR i IN ( SELECT EXTRACTVALUE(VALUE(t), '/PROCREQ/@PROC') proc,
 15                      EXTRACTVALUE(VALUE(t), '/PROCREQ/ACCESS_NBR') access_nbr,
 16                      EXTRACTVALUE(VALUE(t), '/PROCREQ/SOURCE') source,
 17                      EXTRACT(VALUE(t), '/PROCREQ/USERFIELDCD') userfieldcd_xml
 18                 FROM TABLE(XMLSEQUENCE(EXTRACT(x, '/PROCREQ'))) t )
 19    LOOP
 20      dbms_output.put_line('PROC       : ' || i.proc);
 21      dbms_output.put_line('ACCESS_NBR : ' || i.access_nbr);
 22      dbms_output.put_line('SOURCE     : ' || i.source);
 23  
 24      FOR j IN ( SELECT EXTRACTVALUE(VALUE(t), '/USERFIELDCD') userfieldcd
 25                   FROM TABLE(XMLSEQUENCE(EXTRACT(i.userfieldcd_xml, '/USERFIELDCD'))) t )
 26      LOOP
 27        dbms_output.put_line('USERFIELDCD : ' || j.userfieldcd);
 28      END LOOP;
 29    END LOOP;
 30  END;
 31  /
PROC       : 2010
ACCESS_NBR : 145660
SOURCE     : VRU
USERFIELDCD :
USERFIELDCD :

PL/SQL procedure successfully completed.

Note, i've had to create a seperate nested loop to cope with the multiple USERFIELDCD tags, and i've also only extracted certain tags, but you should be able (if this approach is OK for you) to modify to meet your requirements.

Rgds
Re: How to fetch tag data from XML string? [message #162666 is a reply to message #162238] Sun, 12 March 2006 23:46 Go to previous messageGo to next message
b_chugh
Messages: 68
Registered: August 2005
Location: delhi
Member
hello

Thank You so much mchadder it has really helped. I wanted the tag value which is appering mmultiple times. So it has solved that also.

I will appreciate if any one tells me about that parser method also. I dont get to know what is the problem and why it is not fetching the values. It is not giving any compile time errors also.
Re: How to fetch tag data from XML string? [message #162826 is a reply to message #162238] Mon, 13 March 2006 14:48 Go to previous messageGo to next message
mchadder
Messages: 224
Registered: May 2005
Location: UK
Senior Member
Okay, you forced me into it! Wink

You've got a funny XPath expression for extracting your values there. //PARAMETERS/PARAMETER would expect an XML document like :
<PARAMETERS>
  <PARAMETER>...</PARAMETER>
</PARAMETERS>

You need /PROCREQ/ENTITYTYPCD etc. etc, i.e. I've cut down the XML here to prove the point :
SQL> declare
  2    p  XMLPARSER.PARSER;
  3    inout_xml  CLOB := '<PROCREQ><ENTITYTYPCD>PERS</ENTITYTYPCD></PROCREQ>';
  4   docRequest dbms_xmldom.DOMDocument;
  5   lvsEntityTypCd  VARCHAR2(200);
  6  BEGIN
  7  p := xmlparser.newParser;
  8  xmlparser.setValidationMode(p, FALSE);
  9  xmlparser.parseclob(p, inout_XML);
 10  docRequest := xmlparser.getDocument(p);
 11  lvsEntityTypCd := xslprocessor.valueOf(xmldom.makeNode(docRequest),'/PROCREQ/ENTITYTYPCD');
 12  dbms_output.put_line('ENTITY : ' || lvsEntityTypCd);
 13  END;
 14  /
ENTITY : PERS

PL/SQL procedure successfully completed.

Rgds
Re: How to fetch tag data from XML string? [message #162864 is a reply to message #162238] Mon, 13 March 2006 23:10 Go to previous message
b_chugh
Messages: 68
Registered: August 2005
Location: delhi
Member
hi
Thank You so much mchadder. It was so nice of you to tell me the parser method as well.

But now can you tell me how I can fetch the tags which are appearing multiple times using this method.

like

<PROCREQ><ENTITYTYPCD>PERS</ENTITYTYPCD><ENTITYNBR>1290</ENTITYNBR><ENTITYTYPCD>ACCT</ENTITYTYPCD><ENTITYNBR>2340</ENTITYNBR ></PROCREQ>


Sorry for bothering you so much mchadder.


Thanks again for your reply.
Previous Topic: Help me Please
Next Topic: PL/SQL XML extractvalue
Goto Forum:
  


Current Time: Mon Nov 25 08:14:52 CST 2024