decode XML Entity - Android XML

Android examples for XML:XML Entity

Description

decode XML Entity

Demo Code

/*//w ww .  j a v  a2 s. co  m
 * Copyright (c) Mirth Corporation. All rights reserved.
 * 
 * http://www.mirthcorp.com
 * 
 * The software in this package is published under the terms of the MPL license a copy of which has
 * been included with this distribution in the LICENSE.txt file.
 */
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Hashtable;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.log4j.Logger;

public class Main{
    public static void main(String[] argv) throws Exception{
        String entity = "java2s.com";
        System.out.println(decode(entity));
    }
    private static final Hashtable<String, String> decoder = new Hashtable<String, String>(
            300);
    public static String decode(String entity) {
        if (entity.charAt(entity.length() - 1) == ';') // remove trailing
            // semicolon
            entity = entity.substring(0, entity.length() - 1);
        if (entity.charAt(1) == '#') {
            int start = 2;
            int radix = 10;
            if (entity.charAt(2) == 'X' || entity.charAt(2) == 'x') {
                start++;
                radix = 16;
            }
            Character c = new Character((char) Integer.parseInt(
                    entity.substring(start), radix));
            return c.toString();
        } else {
            String s = decoder.get(entity);

            if (s != null)
                return s;
            else
                return "";
        }
    }
}

Related Tutorials