Created
December 18, 2017 14:50
-
-
Save LuisFcoOrtiz/fc4a8f8b3b71a09bfdf0fdef25d0ba35 to your computer and use it in GitHub Desktop.
Java class to export text to XML creating xml file | Clase Java para exportar texto a XML creando un fichero xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package ejercicio8bdoracle; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.swing.JOptionPane; | |
import javax.xml.parsers.*; | |
import javax.xml.transform.OutputKeys; | |
import javax.xml.transform.Transformer; | |
import javax.xml.transform.TransformerConfigurationException; | |
import javax.xml.transform.TransformerException; | |
import javax.xml.transform.TransformerFactory; | |
import javax.xml.transform.dom.DOMSource; | |
import javax.xml.transform.stream.StreamResult; | |
import static jdk.nashorn.internal.runtime.Debug.id; | |
import org.w3c.dom.*; | |
import org.xml.sax.SAXException; | |
/** | |
* | |
* @author manrique | |
*/ | |
public class ExportXML { | |
//variables | |
public DocumentBuilderFactory docFactory; | |
public DocumentBuilder docBuilder; | |
public Document document; | |
public Element elementRoot, column, value; | |
public void initClases() throws SAXException, IOException, ParserConfigurationException { | |
docFactory = DocumentBuilderFactory.newInstance(); | |
docBuilder = docFactory.newDocumentBuilder(); | |
//create elementRoot | |
}//init clases end | |
public void addRootElement( String elementChild) { | |
//ROOT ELEMENT | |
document = docBuilder.newDocument(); | |
elementRoot = document.createElement(elementChild.trim()); | |
document.appendChild(elementRoot); | |
}//add elementRoot | |
public void addColumn(String elementName) { | |
column = document.createElement(elementName.trim()); | |
elementRoot.appendChild(column); | |
}//add element to xml file | |
public void addColumnValue(String elementName, String elementValue) { | |
value = document.createElement(elementName.trim()); | |
value.setTextContent(elementValue.trim()); | |
column.appendChild(value); | |
} | |
public void writeXMLFile(String filePath) throws TransformerConfigurationException, TransformerException { | |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); | |
Transformer transformer = transformerFactory.newTransformer(); | |
//transformer properties | |
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); | |
DOMSource source = new DOMSource(document); | |
File fichero = new File(filePath); | |
//StreamResult result = new StreamResult(new File (filePath)); | |
StreamResult result = new StreamResult(fichero); | |
System.out.println(fichero.getAbsolutePath()); | |
transformer.transform(source, result); | |
}//write all in xml file last step | |
}//end class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment