Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Parse XML file, read a tag with whitespaces value.

user10633184Oct 20 2009 — edited Oct 20 2009
Hi all,
I've got a problem with reading a spaced value record from xml file using sys.xmltype.

My xml file contains the following:
<?xml version = '1.0'?>;
<ROWSET>
<ROW num="1">
<FIRSTNAME>Nik</FIRSTNAME>
<LASTNAME> </LASTNAME>
<AGE>30</AGE>
</ROW>
</ROWSET>


As you see last name contains four spaces!
Now when I'm trying to read value from LASTNAME record I'm getting a NULL value, but I want it to return me those 4 spaces as they are.

Here is a short code description of what I'm doing:

declare
xml sys.xmltype;
str1 varchar2(100);
str2 varchar2(100);
begin
xml := sys.xmltype.createxml(fileContentClob); -- I copy the file content into the fileContentClob variable.

If xml.existsnode('//ROW['1']/LASTNAME') > 0 Then -- This condition evaluates as true
str1 := xml.extract('//ROW['1']/LASTNAME/text()'); -- str1 gets NULL value :(, I want spaces as they are in the file.
str2 := xml.extract('//ROW[' || 1 || ']/LASTNAME').getstringval; -- str2 gets <LASTNAME/> null tag :(.
End If;
end;

Seems like when it createxml from the fileContentClob it ignores the spaces and find LASTNAME as a null field.

Do you have any suggestions on how can I fix that, so I can read whitespaces as they are in the file?


I generate the file also using a xml toolpackage from oracle:

declare
strSqlStmt Varchar2(300);
varCtxHdl dbms_xmlquery.ctxhandle;
varClob Clob;
begin
strSqlStmt := 'SELECT FIRSTNAME,LASTNAME,AGE FROM USERS WHERE ROWNUM =1';

varCtxHdl := dbms_xmlquery.newcontext(strSqlStmt);
varClob := dbms_xmlquery.getxml(varCtxHdl);
dbms_xmlquery.closecontext(varCtxHdl);
End;
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 17 2009
Added on Oct 20 2009
9 comments
660 views