Home » Developer & Programmer » JDeveloper, Java & XML » XML parsing..plz help
XML parsing..plz help [message #178409] Wed, 21 June 2006 01:38 Go to next message
bhagat.singh-j
Messages: 39
Registered: April 2006
Member
Hi,
Can someone help out in parsing the XML below to get the value of uid?
I need to get the value 'bc4184'?

<?xml version="1.0" encoding="UTF-8" ?>
- <GDSDataServlet>
- <request>
<filter>(extshortname=devendran.a)</filter>
<attributes>dn</attributes>
<context />
<scope>subtree</scope>
</request>
- <results>
- <node name="o=world">
- <node name="ou=mnc">
- <node name="ou=people">
<node name="uid=bc4184" />
</node>
</node>
</node>
</results>
</GDSDataServlet>

Thank you!
Bhagat
Re: XML parsing..plz help [message #180444 is a reply to message #178409] Mon, 03 July 2006 09:22 Go to previous messageGo to next message
JRowbottom
Messages: 5933
Registered: June 2006
Location: Sunny North Yorkshire, ho...
Senior Member
The answer is 'Not Easily' (a you may have guesed by the lack of replies.

I've got a solution, but it relies upon the Node element that has the "name" attribute you are looking for being the lowest element with the name "Node", and that this situation marks the node you are looking for.

SQL> set serveroutput on size 10000
SQL> declare
  2    v_xml     xmltype;
  3    v_extract xmltype;
  4  begin
  5  
  6  v_xml := xmltype('<GDSDataServlet>
  7  <request>
  8  <filter>(extshortname=devendran.a)</filter> 
  9  <attributes>dn</attributes> 
 10  <context /> 
 11  <scope>subtree</scope> 
 12  </request>
 13  <results>
 14  <node name="o=world">
 15  <node name="ou=mnc">
 16  <node name="ou=people">
 17  <node name="uid=bc4184" /> 
 18  </node>
 19  </node>
 20  </node>
 21  </results>
 22  </GDSDataServlet>');
 23  
 24  v_xml := xmltype.extract(v_xml,'//node[count(node)=0]/@name');
 25  
 26  dbms_output.put_line(substr(xmltype.getstringval(v_xml),1,255));
 27  end;
 28  /
uid=bc4184
Re: XML parsing..plz help [message #180445 is a reply to message #180444] Mon, 03 July 2006 09:26 Go to previous message
JRowbottom
Messages: 5933
Registered: June 2006
Location: Sunny North Yorkshire, ho...
Senior Member
I spoke too soon. It turns out I can use the "contains" function in a way I hadn't thought of.

This will return you the value of the Name attribute for al elements called "node" which have an attribute called "name" with a value starting with "uid"

SQL> declare
  2    v_xml     xmltype;
  3    v_extract xmltype;
  4  
  5  begin
  6  
  7  v_xml := xmltype('<GDSDataServlet>
  8  <request>
  9  <filter>(extshortname=devendran.a)</filter> 
 10  <attributes>dn</attributes> 
 11  <context /> 
 12  <scope>subtree</scope> 
 13  </request>
 14  <results>
 15  <node name="o=world">
 16  <node name="ou=mnc">
 17  <node name="ou=people">
 18  <node name="uid=bc4184" /> 
 19  </node>
 20  </node>
 21  </node>
 22  </results>
 23  </GDSDataServlet>');
 24  
 25  v_xml := xmltype.extract(v_xml,'//node[starts-with(@name,"uid")]/@name');
 26  
 27  dbms_output.put_line(substr(xmltype.getstringval(v_xml),1,255));
 28  end;
 29  /
uid=bc4184
Previous Topic: XML code to query databse
Next Topic: JInitiator on Pocket PC
Goto Forum:
  


Current Time: Tue Apr 23 14:03:29 CDT 2024