update Xml
//package it.tava.andbudget.data.parser;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlSerializer;
import android.util.Log;
import android.util.Xml;
public class XmlParser {
private final static String TAG = "it.tava.andbudget.xml.XmlParser";
private static final String JAXP = "http://java.sun.com/xml/jaxp/properties/";
private static final String JAXP_SCHEMA_LANGUAGE = JAXP + "schemaLanguage";
private static final String JAXP_SCHEMA_SOURCE = JAXP + "schemaSource";
private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
/**
* Compila il file XML che viene passato come parametro e restituisce il Document DOM
* associato, null altrimenti.
*
* @param xml il file XML di cui effettuare il parsing
* @param xsd il file XSD schema associato all'XML in input
* @param validation se effettuare o meno la validazione del file ottenuto
* @return il Document DOM associato al file in ingresso o null altrimenti
*/
public static Document compileXml(File xml, File xsd, boolean validation) {
Document document = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(validation);
factory.setNamespaceAware(true);
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
factory.setAttribute(JAXP_SCHEMA_SOURCE, xsd);
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(xml);
} catch (SAXException se) {
Log.e(TAG, "Error: compilation failed");
} catch (IOException ioe) {
Log.e(TAG, "Error: IOException");
} catch (ParserConfigurationException e) {
Log.e(TAG, "Error: ParserConfigurationException");
}
return document;
}
public static void updateXml(File xml, File xsd, Document document) {
XmlSerializer serializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
try {
//Writer w = new FileWriter(new File("prova.xml"));
serializer.setOutput(writer);
serializer.startDocument("UTF-8", true);
serializer.startTag("", "budget");
serializer.endTag("", "budget");
serializer.endDocument();
System.out.println(writer.toString());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related examples in the same category