import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.InputSource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
/* http://es.stackoverflow.com/q/21882/127 */
class Ideone
{
{
xml = "<Wrapper>" + xml + "</Wrapper>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(is);
}
{
String texto
= "<!-- quiero eliminar hasta el primer </div> -->\n<DIV id='elid'>soy la primera linea</DIV \n ><div>soy la segunda linea</div>";
//String -> doc
Document doc
= loadXMLFromString
(texto
);
//Construir el serializer y sacar la declaración XML
DOMImplementationLS lsImpl = (DOMImplementationLS)doc.getImplementation().getFeature("LS", "3.0");
LSSerializer lsSerializer = lsImpl.createLSSerializer();
lsSerializer.getDomConfig().setParameter("xml-declaration", false);
//Bucle en todos los nodos de la raíz
Node docRoot = doc.getDocumentElement();
NodeList childNodes = docRoot.getChildNodes();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < childNodes.getLength(); i++) {
if (!divEncontrado) {
//Se encontró?
divEncontrado = childNodes.item(i).getNodeName().equalsIgnoreCase("div");
} else {
//Si se encontró antes, agregarlo al StringBuilder
sb.append(lsSerializer.writeToString(childNodes.item(i)));
}
}
String resultado
= sb.
toString();
System.
out.
println(resultado
); }
}