/** used for cut and paste. */ public void addObjectFromClipboard(String a_value) throws CircularIncludeException { Reader reader = new StringReader(a_value); Document document = null; try { document = UJAXP.getDocument(reader); } catch (Exception e) { e.printStackTrace(); return; } // try-catch Element root = document.getDocumentElement(); if (!root.getNodeName().equals("clipboard")) { return; } // if Node child; for (child = root.getFirstChild(); child != null; child = child.getNextSibling()) { if (!(child instanceof Element)) { continue; } // if Element element = (Element) child; IGlyphFactory factory = GlyphFactory.getFactory(); if (XModule.isMatch(element)) { EModuleInvoke module = (EModuleInvoke) factory.createXModule(element); addModule(module); continue; } // if if (XContour.isMatch(element)) { EContour contour = (EContour) factory.createXContour(element); addContour(contour); continue; } // if if (XInclude.isMatch(element)) { EIncludeInvoke include = (EIncludeInvoke) factory.createXInclude(element); addInclude(include); continue; } // if } // while }
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(); } }