Here you can find the source of getIntegerAttribute(XMLStreamReader in, String attribute)
public static Integer getIntegerAttribute(XMLStreamReader in, String attribute)
//package com.java2s; //License from project: Apache License import static com.google.common.base.Optional.absent; import static com.google.common.base.Optional.fromNullable; import static com.google.common.base.Optional.of; import static com.google.common.base.Throwables.propagate; import static java.lang.Integer.parseInt; import java.io.StringWriter; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.xml.stream.XMLStreamReader; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stax.StAXSource; import javax.xml.transform.stream.StreamResult; import com.google.common.base.Optional; public class Main { public static Integer getIntegerAttribute(String in, String attribute) { return parseInt(getAttribute(in, attribute)); }// w w w . j ava 2s . c o m public static Integer getIntegerAttribute(XMLStreamReader in, String attribute) { return parseInt(getAttribute(in, attribute)); } public static String getAttribute(String in, String attribute) { Optional<String> foundOpt = findAttribute(in, attribute); if (foundOpt.isPresent()) { return foundOpt.get(); } throw new RuntimeException("\"" + attribute + "\" not found in \"" + in + "\""); } public static String getAttribute(XMLStreamReader in, String attribute) { String foundOpt = in.getAttributeValue("", attribute); if (foundOpt == null) { try { throw new RuntimeException("\"" + attribute + "\" not found in:\n" + asString(in)); } catch (Exception e) { propagate(e); } } return foundOpt; } public static Optional<String> findAttribute(String in, String attribute) { Pattern pattern = Pattern.compile(attribute + "='([^']+?)'"); Matcher matcher = pattern.matcher(in); if (matcher.find()) { return of(matcher.group(1)); } pattern = Pattern.compile(attribute + "=\"([^\"]+?)\""); matcher = pattern.matcher(in); if (matcher.find()) { return of(matcher.group(1)); } return absent(); } public static Optional<String> findAttribute(XMLStreamReader in, String attribute) { return fromNullable(in.getAttributeValue("", attribute)); } public static String asString(XMLStreamReader xmlr) throws Exception { Transformer transformer = TransformerFactory.newInstance() .newTransformer(); StringWriter stringWriter = new StringWriter(); transformer.transform(new StAXSource(xmlr), new StreamResult( stringWriter)); return stringWriter.toString(); } }