Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xml-dev] XML parsing the only way?

From: David Tolpin <dvd@----------.--->
To: xml-dev@-----.---.---
Date: 2/3/2004 11:12:00 AM
> I have a bunch of charefs like &#967; and I want to convert those in the 
> approrpiate java string. What is the quickest way of doing this? A new 
> DOMDoc perhaps?
>
> Can someone please share his|her experience and give me a helping hand?

The quickest code for doing this is to write a loop through the string, 
with states for '&', '#', digit and ';'. Should be about twenty lines
in Java at most. I believe it is also the quickest way to write the code.

  static String bunchToString(String bunch) {
    StringBuffer buf=new StringBuffer();
    int cc=0; char ch; int state=0; int i;
    for(i=0;i!=bunch.length();++i) {
      switch(ch=bunch.charAt(i)) {
      case '&': state=1; break;
      case '#': state=state==1?2:0; break;
      case 'x': state=state==2?3:0; break;
      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9': 
        if(state==4||state==5) break;
        state=state==2?4:state==3?5:0; break;
      case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
      case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
        if(state==5) break;
	state=state==3?5:0; break;
      case ';': state=(state==4||state==5)?6:0; break;
      }
      switch(state) {
      case 0: buf.append(ch); break;
      case 1: case 2: case 3: break; 
      case 4: cc=cc*10+ch-'0'; break;
      case 5:
        cc=cc*16+('0'<=ch&&ch<='9'?ch-'0':
	  'A'<=ch&&ch<='F'?ch+10-'A':ch+10-'a'); break;
      case 6: buf.append((char)cc); cc=0; break;
      default: throw new RuntimeException("should not happen");
      }
    }
    return buf.toString();
  }

Untested, but the idea should be clear.
   
David Tolpin
http://davidashen.net/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent