/** * Returns a semantic equivalent of a given XML-encoded message in a default format. * Attributes, comments, and processing instructions are not considered to change the * HL7 meaning of the message, and are removed in the standardized representation. */ public static String standardizeXML(String message) throws SAXException { try { Document doc = XMLUtils.parse(message); clean(doc.getDocumentElement()); return XMLUtils.serialize(doc, true); } catch (Exception e) { throw new RuntimeException("Exception while standardizing XML ", e); } }
/** * Returns a semantic equivalent of a given XML-encoded message in a default format. * Attributes, comments, and processing instructions are not considered to change the * HL7 meaning of the message, and are removed in the standardized representation. */ public static String standardizeXML(String message) throws SAXException { try { Document doc = XMLUtils.parse(message); clean(doc.getDocumentElement()); return XMLUtils.serialize(doc, true); } catch (Exception e) { throw new RuntimeException("Exception while standardizing XML ", e); } }
/** Removes attributes, comments, and processing instructions. */ private static void clean(Element elem) { NodeList children = elem.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE || child.getNodeType() == Node.COMMENT_NODE) { elem.removeChild(child); } else if (child.getNodeType() == Node.ELEMENT_NODE) { clean((Element) child); } } NamedNodeMap attributes = elem.getAttributes(); //get names String[] names = new String[attributes.getLength()]; for (int i = 0; i < names.length; i++) { names[i] = attributes.item(i).getNodeName(); } //remove by name for (int i = 0; i < names.length; i++) { attributes.removeNamedItem(names[i]); } }
/** Removes attributes, comments, and processing instructions. */ private static void clean(Element elem) { NodeList children = elem.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE || child.getNodeType() == Node.COMMENT_NODE) { elem.removeChild(child); } else if (child.getNodeType() == Node.ELEMENT_NODE) { clean((Element) child); } } NamedNodeMap attributes = elem.getAttributes(); //get names String[] names = new String[attributes.getLength()]; for (int i = 0; i < names.length; i++) { names[i] = attributes.item(i).getNodeName(); } //remove by name for (int i = 0; i < names.length; i++) { attributes.removeNamedItem(names[i]); } }