Transforms an escaped XML into the original, "un-escaped" value (for example turn &lt;Hello&gt; into <Hello>) - Java XML

Java examples for XML:XML String Escape

Description

Transforms an escaped XML into the original, "un-escaped" value (for example turn &lt;Hello&gt; into <Hello>)

Demo Code

/*//from  ww  w.java 2s  .c  om
 * Copyright (C) 2012 Zach Melamed
 * 
 * Latest version available online at https://github.com/zach-m/jonix
 * Contact me at zach@tectonica.co.il
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Element;

public class Main{
    public static void main(String[] argv) throws Exception{
        String escaped = "java2s.com";
        System.out.println(unescape(escaped));
    }
    /**
     * Transforms an escaped XML into the original, "un-escaped" value (for example turn &amp;lt;Hello&amp;gt; into
     * &lt;Hello&gt;)
     * 
     * @param escaped
     *            the escaped XML string
     * @return the un-escaped XML string
     * @throws XMLStreamException
     */
    public static String unescape(String escaped) throws XMLStreamException {
        if (escaped == null)
            return null;
        XMLStreamReader reader = XmlChunkerContext.inputFactory
                .createXMLStreamReader(new StringReader("<xml>" + escaped
                        + "</xml>"));
        StringWriter sw = new StringWriter(escaped.length());
        while (reader.hasNext()) {
            reader.next();
            if (reader.hasText())
                sw.append(reader.getText());
        }
        return sw.toString();
    }
}

Related Tutorials