public static synchronized String domToString(Document theDoc, Node theNode) { DOMImplementation domI = theDoc.getImplementation(); DOMImplementationLS domIls = (DOMImplementationLS) domI.getFeature("LS", "3.0"); LSSerializer lss = domIls.createLSSerializer(); String xmlstr = lss.writeToString(theNode); // <?xml version="1.0" encoding="UTF-16"?> return xmlstr; }
/** Transform into formatted XML. */ public static String getFormattedXml(Node node) { Document document = node.getOwnerDocument().getImplementation().createDocument("", "fake", null); Element copy = (Element) document.importNode(node, true); document.importNode(node, false); document.removeChild(document.getDocumentElement()); document.appendChild(copy); DOMImplementation domImpl = document.getImplementation(); DOMImplementationLS domImplLs = (DOMImplementationLS) domImpl.getFeature("LS", "3.0"); LSSerializer serializer = domImplLs.createLSSerializer(); serializer.getDomConfig().setParameter("format-pretty-print", true); return serializer.writeToString(document); }
public void connectXML(String port, String user, String pass) throws ParserConfigurationException { String filename = "connect.xml"; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element body = doc.createElement("Connect"); doc.appendChild(body); Element e = doc.createElement("Port"); Text t = doc.createTextNode(port); e.appendChild(t); body.appendChild(e); Element e1 = doc.createElement("User"); Text t1 = doc.createTextNode(user); e1.appendChild(t1); body.appendChild(e1); Element e2 = doc.createElement("Pass"); Text t2 = doc.createTextNode(pass); e2.appendChild(t2); body.appendChild(e2); DOMImplementation impl = doc.getImplementation(); DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0"); LSSerializer ser = implLS.createLSSerializer(); LSOutput lsOutput = implLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); // System.out.println(lsOutput.toString()); Writer stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); ser.write(doc, lsOutput); String result = stringWriter.toString(); System.out.println(result); // System.out.println(ser.write(doc,lsOutput)); try { print(filename, result); System.out.println("Print Success"); File file = new File(filename); System.out.println(file.getCanonicalPath()); } catch (IOException e3) { // TODO Auto-generated catch block e3.printStackTrace(); System.out.println("print failed"); } }
/** * Converts a dom element to a String * * @param node * @return the dom as a String */ public static String writeDomToString(Element node) { DOMImplementation domImplementation = node.getOwnerDocument().getImplementation(); if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) { DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS", "3.0"); LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); LSOutput lsOutput = domImplementationLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(node, lsOutput); return stringWriter.toString(); } else { throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported."); } }
private LSInput getLSInput() throws Exception { DOMImplementationLS impl; DOMImplementation docImpl = builder.getDOMImplementation(); // Try to get the DOMImplementation from doc first before // defaulting to the sun implementation. if (docImpl != null && docImpl.hasFeature("LS", "3.0")) { impl = (DOMImplementationLS) docImpl.getFeature("LS", "3.0"); } else { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); if (impl == null) { System.setProperty( DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl"); registry = DOMImplementationRegistry.newInstance(); impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); } } return impl.createLSInput(); }
/** * Create the xml files for communities * * @param f * @param communities * @throws IOException */ public void generateCommunityXMLFile(File f, Collection<Community> communities) throws IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new IllegalStateException(e); } DOMImplementation impl = builder.getDOMImplementation(); DOMImplementationLS domLs = (DOMImplementationLS) impl.getFeature("LS", "3.0"); LSSerializer serializer = domLs.createLSSerializer(); LSOutput lsOut = domLs.createLSOutput(); Document doc = impl.createDocument(null, "communities", null); Element root = doc.getDocumentElement(); FileOutputStream fos; OutputStreamWriter outputStreamWriter; BufferedWriter writer; try { fos = new FileOutputStream(f); try { outputStreamWriter = new OutputStreamWriter(fos, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } writer = new BufferedWriter(outputStreamWriter); lsOut.setCharacterStream(writer); } catch (FileNotFoundException e) { throw new IllegalStateException(e); } // create XML for the communities for (Community c : communities) { Element community = doc.createElement("community"); Element id = doc.createElement("id"); Text data = doc.createTextNode(c.id.toString()); id.appendChild(data); community.appendChild(id); Element name = doc.createElement("name"); data = doc.createTextNode(c.name); name.appendChild(data); community.appendChild(name); Element introductoryText = doc.createElement("introductory_text"); data = doc.createTextNode(c.introductoryText); introductoryText.appendChild(data); community.appendChild(introductoryText); Element sideBarText = doc.createElement("side_bar_text"); data = doc.createTextNode(c.sideBarText); sideBarText.appendChild(data); community.appendChild(sideBarText); Element copyright = doc.createElement("copyright"); data = doc.createTextNode(c.copyright); copyright.appendChild(data); community.appendChild(copyright); Element logoFileName = null; if (c.logoFileInfo != null) { logoFileName = doc.createElement("logo_file_name"); data = doc.createTextNode(c.logoFileInfo.newFileName); logoFileName.appendChild(data); community.appendChild(logoFileName); } root.appendChild(community); if (c.links != null && c.links.size() > 0) { Element links = doc.createElement("links"); // add the links for (Link l : c.links) { Element link = doc.createElement("link"); Element linkName = doc.createElement("name"); data = doc.createTextNode(l.name); linkName.appendChild(data); Element linkUrl = doc.createElement("url"); data = doc.createTextNode(l.value); linkUrl.appendChild(data); link.appendChild(linkName); link.appendChild(linkUrl); links.appendChild(link); } community.appendChild(links); } if (c.groupPermissions != null && c.groupPermissions.size() > 0) { Element groupPermissions = doc.createElement("group_permissions"); // add the links for (GroupPermission p : c.groupPermissions) { Element groupPermission = doc.createElement("group_permission"); Element permissionAction = doc.createElement("action_id"); data = doc.createTextNode(p.action + ""); permissionAction.appendChild(data); groupPermission.appendChild(permissionAction); Element groupId = doc.createElement("group_id"); data = doc.createTextNode(p.groupId.toString()); groupId.appendChild(data); groupPermission.appendChild(groupId); groupPermissions.appendChild(groupPermission); } community.appendChild(groupPermissions); } if (c.epersonPermissions != null && c.epersonPermissions.size() > 0) { Element epersonPermissions = doc.createElement("eperson_permissions"); // add the links for (EpersonPermission p : c.epersonPermissions) { Element epersonPermission = doc.createElement("eperson_permission"); Element permissionAction = doc.createElement("action_id"); data = doc.createTextNode(p.action + ""); permissionAction.appendChild(data); epersonPermission.appendChild(permissionAction); Element epersonId = doc.createElement("eperson_id"); data = doc.createTextNode(p.epersonId.toString()); epersonId.appendChild(data); epersonPermission.appendChild(epersonId); epersonPermissions.appendChild(epersonPermission); } community.appendChild(epersonPermissions); } } serializer.write(root, lsOut); try { fos.close(); writer.close(); outputStreamWriter.close(); } catch (Exception e) { throw new IllegalStateException(e); } }
/** * Create the xml file for the set of collections. * * @param xmlFile - file to write the xml to * @param contributor types - set of contributor types to export * @throws IOException - if writing to the file fails. */ public void createXmlFile(File xmlFile, Collection<ContributorType> contributorTypes) throws IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; String path = FilenameUtils.getPath(xmlFile.getCanonicalPath()); if (!path.equals("")) { File pathOnly = new File(FilenameUtils.getFullPath(xmlFile.getCanonicalPath())); FileUtils.forceMkdir(pathOnly); } if (!xmlFile.exists()) { if (!xmlFile.createNewFile()) { throw new IllegalStateException("could not create file"); } } try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new IllegalStateException(e); } DOMImplementation impl = builder.getDOMImplementation(); DOMImplementationLS domLs = (DOMImplementationLS) impl.getFeature("LS", "3.0"); LSSerializer serializer = domLs.createLSSerializer(); LSOutput lsOut = domLs.createLSOutput(); Document doc = impl.createDocument(null, "contributor_types", null); Element root = doc.getDocumentElement(); FileOutputStream fos; OutputStreamWriter outputStreamWriter; BufferedWriter writer; try { fos = new FileOutputStream(xmlFile); try { outputStreamWriter = new OutputStreamWriter(fos, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } writer = new BufferedWriter(outputStreamWriter); lsOut.setCharacterStream(writer); } catch (FileNotFoundException e) { throw new IllegalStateException(e); } // create XML for the child collections for (ContributorType ct : contributorTypes) { Element contributorType = doc.createElement("contributor_type"); this.addIdElement(contributorType, ct.getId().toString(), doc); this.addNameElement(contributorType, ct.getName(), doc); this.addDescription(contributorType, ct.getDescription(), doc); this.addSystemCode(contributorType, ct.getUniqueSystemCode(), doc); } serializer.write(root, lsOut); try { fos.close(); writer.close(); outputStreamWriter.close(); } catch (Exception e) { throw new IllegalStateException(e); } }