This forum is currently locked. You can't register or post questions at this time. (read more)

parse XML from URL - looping problem

All posts relating to Oracle PL/SQL development.

Moderator: Tim...

Guest

parse XML from URL - looping problem

Postby Guest » Mon Nov 01, 2004 6:09 pm

I have been able to parse an xml doc from a URL, however I need some help with how to loop within the document.
It is very straightforward - i have an orders xml doc that can have multiple line items. The doc is like:
<ORDERS>
<ORDER>
<DATE>2004-10-12 11:30:07</DATE>
<ID>957</ID>
<NAME>John Doe</NAME>
<COMPANY>none</COMPANY>
<STREET>100 Elm Street</STREET>
<SUBURB />
<CITY>Pittsburgh</CITY>
<STATE>PA</STATE>
<ZIP>15222</ZIP>
<COUNTRY>United States</COUNTRY>
<ORDERINFO>
<QUANTITY>1</QUANTITY>
<MODEL>76</MODEL>
<PRODUCT>Cooler Organizer(tm)</PRODUCT>
<QUANTITY>5</QUANTITY>
<MODEL>87</MODEL>
<PRODUCT>Chrome CupHolder</PRODUCT>
</ORDERINFO>
</ORDER>
</ORDERS>

So I need to be able to loop within the <ORDERINFO> and pick up the multiple line items for the customers order.

I have a PLSQL block that i'm working with that does the trick except I do not have the ORDERINFO looping in yet - that's where I need help. Here is my code:
declare
orderURL VARCHAR2(80);
parser xmlparser.Parser;
orderXML xmldom.DOMDocument;
orders xmldom.DOMNodeList;
orders_found NUMBER;
curNode xmldom.DOMNode;
textName xmldom.DOMNode;

begin
dbms_output.put_line( 'Orders processed on '||to_char(sysdate, 'YYYY-MON-DD'));

orderURL := 'http://localhost:7777/xml.php';

parser := xmlparser.newParser ;
orderXML := xmlparser.parse( orderURL );
xmlparser.freeParser( parser );

orders := xmldom.getElementsByTagName( orderXML, 'NAME' );

-- loop for customer
for x in 1..xmldom.getLength(orders) loop
curNode := xmldom.item( orders, x-1 );
textName := xmldom.getFirstChild( curNode );

dbms_output.put_line( 'Customer: '||xmldom.getNodeValue( textName ) );


-- loop within the order to get products ordered
-- for y in 1..get loop
-- end loop;

end loop;

xmldom.freeDocument( orderXML );

end;
/

Thanks in advance !!

Tim...
Site Admin
Posts: 18437
Joined: Mon Nov 01, 2004 5:56 pm
Location: England, UK
Contact:

RE:parse XML from URL - looping problem

Postby Tim... » Mon Nov 01, 2004 6:09 pm

Hi.

I would not consider this a "proper" xml document as you have a repeating sequence of data within a single paranet tag:

<ORDERINFO>
<QUANTITY>1</QUANTITY>
<MODEL>76</MODEL>
<PRODUCT>Cooler Organizer(tm)</PRODUCT>
<QUANTITY>5</QUANTITY>
<MODEL>87</MODEL>
<PRODUCT>Chrome CupHolder</PRODUCT>
</ORDERINFO>

I would do something like this:

<ORDERINFO>
<ORDERLINE>
<QUANTITY>1</QUANTITY>
<MODEL>76</MODEL>
<PRODUCT>Cooler Organizer(tm)</PRODUCT>
</ORDERLINE>
<ORDERLINE>
<QUANTITY>5</QUANTITY>
<MODEL>87</MODEL>
<PRODUCT>Chrome CupHolder</PRODUCT>
</ORDERLINE>
</ORDERINFO>

This way you can loop through the orderlines and parse their contents.

Cheers

Tim...
Tim...
Oracle ACE Director
Oracle ACE of the Year 2006 - Oracle Magazine Editors Choice Awards
OakTable Member
OCP DBA 7.3, 8, 8i, 9i, 10g, 11g
OCP Advanced PL/SQL Developer
Oracle Database: SQL Certified Expert
My website: http://www.oracle-base.com
My blog: http://www.oracle-base.com/blog


Return to “Oracle SQL and PL/SQL Development”

Who is online

Users browsing this forum: No registered users and 3 guests

cron