import java.io.StringWriter;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.Serializer;
import org.apache.xml.serialize.DOMSerializer;
import org.apache.xml.serialize.SerializerFactory;
import org.apache.xml.serialize.XMLSerializer;
public class HelloApache2
{
public static void main (String[] args)
{
try
{
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(args[0]);
OutputFormat format = new OutputFormat (doc);
StringWriter stringOut = new StringWriter ();
XMLSerializer serial = new XMLSerializer (stringOut,
format);
serial.serialize(doc);
System.out.println(stringOut.toString());
}
catch (FactoryConfigurationError e)
{
System.out.println
("Unable to get a document builder factory: " + e);
}
catch (ParserConfigurationException e)
{
System.out.println("Parser was unable to be configured: "
+ e);
}
catch (SAXException e)
{
System.out.println("Parsing error: " + e);
}
catch (IOException e)
{
System.out.println("I/O error: " + e);
}
}
}
samedi 1 septembre 2012
Java XML to string
Inscription à :
Publier les commentaires (Atom)
Aucun commentaire:
Enregistrer un commentaire