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

Simon Metson simon at senserecords.com
Thu Jul 8 17:18:15 PDT 2004


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
>
>




More information about the jdom-interest mailing list