Searches an XML string for an entity definition and returns its value. - Java XML

Java examples for XML:XML String

Description

Searches an XML string for an entity definition and returns its value.

Demo Code

/*---------------------------------------------------------------
 *  Copyright 2005 by the Radiological Society of North America
 *
 *  This source software is released under the terms of the
 *  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
 *----------------------------------------------------------------*/


import java.io.StringWriter;

public class Main{

    /**//ww  w.ja  v a  2  s  .  c  om
     * Searches an XML string for an entity definition
     * and returns its value.
     * @param xmlString the XML string to search.
     * @param theName the name of the entity to find.
     * @return the value of the named entity.
     */
    public static String getEntity(String xmlString, String theName) {
        int begin = -1;
        int end;
        String name, value;
        while ((begin = xmlString.indexOf("!ENTITY", begin + 1)) != -1) {
            begin = StringUtil.skipWhitespace(xmlString, begin + 7);
            end = StringUtil.findWhitespace(xmlString, begin);
            name = xmlString.substring(begin, end);
            if (name.equals(theName)) {
                begin = xmlString.indexOf("\"", end) + 1;
                end = xmlString.indexOf("\"", begin);
                return xmlString.substring(begin, end);
            }
        }
        return "";
    }
}

Related Tutorials