Skip to content

Instantly share code, notes, and snippets.

@HashRaygoza
Last active February 18, 2021 04:15
Show Gist options
  • Save HashRaygoza/e686f8c07eb27f61aa7cb4abe0787575 to your computer and use it in GitHub Desktop.
Save HashRaygoza/e686f8c07eb27f61aa7cb4abe0787575 to your computer and use it in GitHub Desktop.
File archivo = new File(file);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
// Esto es para agilizar la lectura de archivos grandes
documentBuilderFactory.setNamespaceAware(false);
documentBuilderFactory.setValidating(false);
documentBuilderFactory.setFeature("http://xml.org/sax/features/namespaces", false);
documentBuilderFactory.setFeature("http://xml.org/sax/features/validation", false);
documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
// constructor de objetos XML
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document documento = documentBuilder.parse(archivo);
// Esto ayuda al procesamiento
documento.getDocumentElement().normalize();
StringWriter writer = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(new DOMSource(documento), new StreamResult(writer));
String xmlString = writer.getBuffer().toString();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment