• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

Efficient text to xml test

I would like to write a method or class that tests to see if, for example, text that makes up a specific row/record in my Oracle DB can be succcesfully converted to xml.  I am aware that there may be special characters in text that may cause problems when converting to xml, so this is the reason for this test.  I am looking for an efficient way to run such a test.  Any ideas?
0
rnicholus
Asked:
rnicholus
  • 6
  • 4
  • 4
2 Solutions
 
aozarovCommented:
the special characters are: &, <  > ' and "
You can loop thru your text and check each caracter against a java swith statement that cotains the above characters.
char[] chars = string.toCharArray();
for (int i =; i < chars; i++)
{
char ch = chars[i];
switch (ch)
{
case '&':
case '<':
...
return false;
}
}
0
 
objectsCommented:
Why not instead just wrap the data in CDATA block?
0
 
rnicholusAuthor Commented:
I'm not sure I even know what a CDATA block is.  Could you enlighten me?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
aozarovCommented:
XML content that is wrapped inside CDATA will not be parsed as XML and will be left as is:
e.g.
<script>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
   {
   return 1
   }
else
   {
   return 0
   }
}
]]>
</script>

see http://www.w3schools.com/xml/xml_cdata.asp
0
 
rnicholusAuthor Commented:
Thanks.  I will investigate this futher tommorrow (if time allows).
0
 
rnicholusAuthor Commented:
I still have not had time to invesitage this, but I will close this out anyways.  If I understand correctly, if I wrap a string inside a CDATA block, it is guaranteed to not have problems when injecting into into an XML doc.  Correct?
0
 
aozarovCommented:
Correct (to check it you can take that piece of XML and try to open in with any XML editor, Windows Explorer can also be used).
If the XML is not well formed you will get exception (try to take out the CDATA section and see the difference).
0
 
rnicholusAuthor Commented:
Thanks!
0
 
aozarovCommented:
:-)
0
 
objectsCommented:
Good to see my suggestion help you :)
0
 
rnicholusAuthor Commented:
oops.  I apologize.  I should have split the points.  Is there any way I can do this now?
0
 
objectsCommented:
0
 
rnicholusAuthor Commented:
done.  hopefully the admins will take care of this post-haste.

http://www.experts-exchange.com/Community_Support/Q_21407273.html
0
 
objectsCommented:
thanks :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now