Code example for DocumentBuilder

Methods: newDocument

0
                    // node is a w3c element - create a w3c document                     
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                    factory.setNamespaceAware(true);
                    DocumentBuilder builder = factory.newDocumentBuilder();
                    // add missing xsi namespace 
                    Document w3cDoc = builder.newDocument();
                    Node dup = w3cDoc.importNode(node, true);
                    w3cDoc.appendChild(dup);
                    // print document 
                    OutputFormat xmlFormat = new OutputFormat("xml","ISO-8859-1", true);
                    xmlFormat.setOmitXMLDeclaration(true);
                    XMLSerializer serializer = new XMLSerializer(stringWriter, xmlFormat);
                    serializer.serialize(w3cDoc);
                     
                    value = stringWriter.toString();
                } catch (ParserConfigurationException e) { 
                    e.printStackTrace();
                }   catch (IOException e) {
                    e.printStackTrace();
                }  
            }