Project

Profile

Help

Bug #4937 ยป testSaxonTransformer.java

Taras Chervinka, 2021-03-15 14:36

 
1
import org.w3c.dom.Document;
2
import org.xml.sax.InputSource;
3
import org.xml.sax.SAXException;
4

    
5
import javax.xml.parsers.DocumentBuilder;
6
import javax.xml.parsers.DocumentBuilderFactory;
7
import javax.xml.parsers.ParserConfigurationException;
8
import javax.xml.transform.*;
9
import javax.xml.transform.dom.DOMSource;
10
import javax.xml.transform.stream.StreamResult;
11
import java.io.IOException;
12
import java.io.StringReader;
13
import java.io.StringWriter;
14

    
15
public class testSaxonTransformer
16
{
17

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

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

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

    
30
      StringWriter writer = new StringWriter(512);
31

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

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

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

    
41
   }
42
}
    (1-1/1)