private static void attributes2String(Node elm, StringBuffer sb) { NamedNodeMap attributes = elm.getAttributes(); if (attributes != null && attributes.getLength() > 0) { sb.append(" "); for (int j = 0; j < attributes.getLength(); j++) { sb.append( String.format( "%s=\"%s\"", attributes.item(j).getNodeName(), StringHelper.escapeXml(attributes.item(j).getNodeValue(), "<&\""))); if (j < attributes.getLength() - 1) { sb.append(" "); } } } }
private static StringBuffer nodeAsText(Node elm, StringBuffer sb, boolean ignoreComments) { if (elm.getNodeType() == Node.CDATA_SECTION_NODE) { CDATASection cdata = (CDATASection) elm; sb.append("<![CDATA["); sb.append(cdata.getData()); sb.append("]]>"); return sb; } if (elm.getNodeType() == Node.COMMENT_NODE) { if (ignoreComments) { return sb; } Comment c = (Comment) elm; sb.append("<!--"); sb.append(c.getData()); sb.append("-->"); return sb; } if (elm.getNodeType() == Node.TEXT_NODE) { Text t = (Text) elm; sb.append(StringHelper.escapeXml(t.getData(), "<&")); return sb; } NodeList childs = elm.getChildNodes(); sb.append("<" + elm.getNodeName()); attributes2String(elm, sb); if (childs.getLength() > 0) { sb.append(">"); for (int i = 0; i < childs.getLength(); i++) { Node child = childs.item(i); nodeAsText(child, sb, ignoreComments); } sb.append("</" + elm.getNodeName() + ">"); } else { sb.append("/>"); } return sb; }