I was working on a 2 versions of a BPM product the older one without
support for namespaces and the newer one supporting namespaces. The
messages from external systems were with namespaces. When the old system
was called it was required to strip the namespaces. Initially we
thought of using xslt to achieve this. But then again regular expression
came to our rescue. The cute little function below can remove
namespaces from xml string
public static String removeXmlStringNamespaceAndPreamble(String xmlString) {
return xmlString.replaceAll("(<\\?[^<]*\\?>)?", ""). /* remove preamble */
replaceAll("xmlns.*?(\"|\').*?(\"|\')", "") /* remove xmlns declaration */
.replaceAll("(<)(\\w+:)(.*?>)", "$1$3") /* remove opening tag prefix */
.replaceAll("(</)(\\w+:)(.*?>)", "$1$3"); /* remove closing tags prefix */
}
Love the power of regular expression :)
No comments:
Post a Comment