예제 #1
0
 public void toSave() {
   try {
     TransformerFactory tf = TransformerFactory.newInstance();
     Transformer transformer = tf.newTransformer();
     DOMSource source = new DOMSource(document);
     transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
     transformer.setOutputProperty(OutputKeys.INDENT, "yes");
     PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
     StreamResult result = new StreamResult(pw);
     transformer.transform(source, result);
   } catch (TransformerException mye) {
     mye.printStackTrace();
   } catch (IOException exp) {
     exp.printStackTrace();
   }
 }
 /**
  * Writes an XML file from a Document object.
  *
  * @param doc the Document object to be written to file
  * @param file the file to be written
  * @throws IOException
  * @author Klaus Meffert
  * @since 2.0
  */
 public static void writeFile(Document doc, File file) throws IOException {
   // Use a Transformer for output
   TransformerFactory tFactory = TransformerFactory.newInstance();
   Transformer transformer;
   try {
     transformer = tFactory.newTransformer();
   } catch (TransformerConfigurationException tex) {
     throw new IOException(tex.getMessage());
   }
   DOMSource source = new DOMSource(doc);
   FileOutputStream fos = new FileOutputStream(file);
   StreamResult result = new StreamResult(fos);
   try {
     transformer.transform(source, result);
     fos.close();
   } catch (TransformerException tex) {
     throw new IOException(tex.getMessage());
   }
 }
예제 #3
0
  public static boolean writeXML(File file, Document document) {

    javax.xml.transform.Transformer transformer = null;
    try {
      transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
      e.printStackTrace();
      return false;
    }

    transformer.setOutputProperty("indent", "yes");
    transformer.setOutputProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "2");
    transformer.setOutputProperty("encoding", "UTF-8");

    try {
      transformer.transform(new DOMSource(document), new StreamResult(file));
    } catch (TransformerException e) {
      e.printStackTrace();
      return false;
    }

    return true;
  }
예제 #4
0
  void xml() {
    try {
      File file = new File("c:\\users\\jason\\desktop\\proxies.xml");

      // Create instance of DocumentBuilderFactory
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

      // Get the DocumentBuilder
      DocumentBuilder docBuilder = factory.newDocumentBuilder();

      // Using existing XML Document
      Document doc = docBuilder.parse(file);

      // normalize the text
      doc.getDocumentElement().normalize();

      // gets just the name of the root
      String simple_root = doc.getDocumentElement().getNodeName();

      Element root = doc.getDocumentElement();

      // gets the ip elements
      NodeList proxy = doc.getElementsByTagName("proxy");

      // checks the make sure i got the ip elements by printing out the number of occurances
      int total = proxy.getLength();

      NodeList list = doc.getElementsByTagName("*");
      System.out.println("\nElements in the proxy file:");
      int proxy_num = 1;
      for (int i = 0; i < list.getLength(); i++) {
        Node num2 = proxy.item(i);
        Element second = (Element) num2;
        Element element2 = (Element) list.item(i);
        NodeList text2 = element2.getChildNodes();
        if (element2.getNodeName() != "proxies" && element2.getNodeName() != "proxy") {
          if (element2.getNodeName() == "ip") {
            System.out.println("");
            System.out.println("Proxy #: " + proxy_num);
            proxy_num++;
            System.out.println(
                element2.getNodeName() + ": " + ((Node) text2.item(0)).getNodeValue().trim());

          } else
            System.out.println(
                element2.getNodeName() + ": " + ((Node) text2.item(0)).getNodeValue().trim());
          if (element2.getNodeName() == "source-ip") {
            ip = ((Node) text2.item(0)).getNodeValue().trim();
            myArr.add(ip);
            jComboBox.addItem(ip);
          }
        }
      }

      // set up a transformer
      TransformerFactory transfac = TransformerFactory.newInstance();
      Transformer trans = transfac.newTransformer();

      // create string from xml tree
      StringWriter sw = new StringWriter();
      StreamResult result = new StreamResult(sw);
      DOMSource source = new DOMSource(doc);
      trans.transform(source, result);
      String xmlString = sw.toString();

      OutputStream f0;
      byte buf[] = xmlString.getBytes();
      f0 = new FileOutputStream("c:\\users\\jason\\desktop\\connections.xml");
      for (int i = 0; i < buf.length; i++) {
        f0.write(buf[i]);
      }
      f0.close();
      buf = null;
    } catch (SAXException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ParserConfigurationException e) {
      e.printStackTrace();
    } catch (TransformerConfigurationException e) {
      e.printStackTrace();
    } catch (TransformerException e) {
      e.printStackTrace();
    }
  }