[jdom-interest] Re: Removing illegal characters from element names

Simon Metson simon at senserecords.com
Fri Jul 9 06:53:09 PDT 2004


Hi Cecil,
	The problem was that the illegal XML characters were being used as
element names (ie Elements were named after table column names), so I
needed to replace any illegal characters with legal ones, eg:
<column1 + column2>blah</column1 + column2>
becomes
<column1___column2>blah</column1___column2>
Cheers
Simon

On Fri, 2004-07-09 at 12:00, New, Cecil (GEAE) wrote:
> I think you could also wrap the sql statement with a CDATA section tag:
> <processed_sql>
> 	<![CDATA[ *** sql with illegal chars here *** ]]>
> </processed_sql>
> 
> -----Original Message-----
> From: Simon Metson [mailto:simon at senserecords.com]
> Sent: Thursday, July 08, 2004 8:18 PM
> To: jdom-interest at jdom.org
> Subject: [jdom-interest] Re: Removing illegal characters from element
> names
> 
> 
> Hi again,
>     Have answered my own question: Verifier.checkXML with a bit of 
> regexp'ing;
> 
> if (Verifier.checkXMLName(colName)=="XML names cannot be null or empty"){
>     colName="col"+cCount;
> }
> while(Verifier.checkXMLName(colName)!=null){
>     String[] splitResult = Verifier.checkXMLName(colName).split("\"");
>     System.out.println(splitResult[0]);
>     System.out.println("duff character: \"" + splitResult[1] + "\"");
>                        
>     //"XML names cannot contain the character \"" + c + "\"";
>     colName = colName.replaceAll("\\" + splitResult[1], "_");
>     System.out.println(colName);
> }
> 
> Cheers
> Simon
> 
> Simon Metson wrote:
> 
> > Hi All,
> >    I'm new to the list so I appologise if this has already been 
> > answered but after trawling Google for an hour I can't find the info I 
> > need. I'm writing a class that takes the result of any sql statement 
> > and returns a JDOM xml document with elements named after the column 
> > name, something like;
> > <results>
> >  <processed_sql>SELECT * from test where field1 like 
> > '%h%';</processed_sql>
> >  <result_set>
> >    <row>
> >      <field1>hello</field1>
> >      <field2>world</field2>
> >    </row>
> >    <row>
> >      <field1>blah</field1>
> >      <field2>eggs</field2>
> >    </row>
> >  </result_set>
> > </results>
> >
> > as the sql can be any sql there is a possibility of illegal characters 
> > eg doing;
> >
> >    SELECT field1, field2, field1 + field2 from test where field1 like 
> > '%h%';
> >
> > will throw;
> >
> > org.jdom.IllegalNameException: The name "field1_+_field2" is not legal 
> > for JDOM/XML elements: XML names cannot contain the character "+".
> >
> > (as you can see I already replace the space with underscore). Is there 
> > a quick way of replacing all illegal characters in my element names? 
> > My current plan is to take a look at the JDOM source to work out a 
> > regexp to fix this, any better suggestions?
> > Cheers
> > Simon
> >
> >
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
> t.com
-- 
---------contact details---------
  p: 0117 9288760
  e: s.metson at bristol.ac.uk
  w: www.metsonet.co.uk

  Office 4.51
  High Energy Particle Physics Group

  University of Bristol
  H H Wills Physics Laboratory
  Tyndall Avenue
  BRISTOL
  BS8 1TL




More information about the jdom-interest mailing list