Project

Profile

Help

Bug #4937 ยป testSaxonTransformer.java

Taras Chervinka, 2021-03-15 14:36

 
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

public class testSaxonTransformer
{

public static void main(String[] args)
throws ParserConfigurationException, TransformerException, IOException, SAXException
{
String xml = "<a xmlns='someNS' name='test'><b xmlns=''/></a>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);

DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xml)));

Transformer tf = TransformerFactory.newInstance().newTransformer();

StringWriter writer = new StringWriter(512);

tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); //$NON-NLS-1$ //$NON-NLS-2$
tf.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ //$NON-NLS-2$
tf.transform(new DOMSource(doc.getDocumentElement()), new StreamResult(writer));

String transformed = writer.toString();
System.out.println(writer.toString());

Document doc2 = builder.parse(new InputSource(new StringReader(transformed)));

}
}
    (1-1/1)