---
title: "J2ME转义xml实现"
author: "showcup"
date: "2010-01-07T20:20:10Z"
tags: []
categories: ["编程语言"]
slug: "j2me-xml"
draft: false
lastmod: "2010-01-08T20:24:45Z"
id: 565250
url: "https://showcup.iteye.com/blog/565250"
---

由于J2ME中的String没有replace(String ,String)方法，要对xml进行转义很难通过s.replace("&","&amp;");无奈之下自己实现
//转义
   public static String escapeXml(String s)
   {
      StringBuffer f = new StringBuffer();
      char arr = s.toCharArray();
      for (int i = 0; i < arr.length; i++)
      {
         char ch = arr*;
         if (ch == '&')
         {
            f.append("&amp;");
         }
         else if (ch == '<')
         {
            f.append("&lt;");
         }
         else if (ch == '>')
         {
            f.append("&gt;");
         }
         else if (ch == '\'')
         {
            f.append("&apos;");
         }
         else if (ch == '\"')
         {
            f.append("&quot;");
         }else{
            f.append(ch);
         }
      }
      return f.toString();
   }