Java examples for java.lang:String XML
Find the string value of an attribute starting at position k in a text string, returning the string value of the attribute, or null if the attribute is not present.
/*--------------------------------------------------------------- * 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) *----------------------------------------------------------------*/ //package com.java2s; public class Main { /**//from ww w . ja va 2s.c o m * Find the string value of an attribute starting at position k in * a text string, returning the string value of the attribute, or * null if the attribute is not present. Note: this method returns * the value of the attribute with normalized whitespace. * @param text the XML string. * @param k the starting point in the XML string to search for the attribute. * @param attr the name of the attribute. * @return the string value of the attribute, or null if the attribute is not * present. */ public static String getAttribute(String text, int k, String attr) { int kk = text.indexOf(">", k); if (kk < 0) return null; String t = text.substring(k, kk).replaceAll("\\s+", " ") .replaceAll(" =", "=").replaceAll("= ", "="); k = t.indexOf(" " + attr + "="); if (k < 0) return null; k = t.indexOf("=", k) + 2; if (t.charAt(k - 1) != '"') return null; kk = t.indexOf("\"", k); if (kk < 0) return null; return t.substring(k, kk); } }