List of usage examples for org.apache.commons.jxpath.xml DOMParser parseXML
public Object parseXML(InputStream stream)
From source file:net.sbbi.upnp.JXPathParser.java
public Object parseXML(InputStream in) { StringBuffer xml = new StringBuffer(); try {/* ww w. j a v a 2s. co m*/ byte[] buffer = new byte[512]; int readen = 0; while ((readen = in.read(buffer)) != -1) { xml.append(new String(buffer, 0, readen)); } } catch (IOException ex) { log.error("IOException occured during XML reception", ex); return null; } String doc = xml.toString(); log.debug("Readen raw xml doc:\n" + doc); if (doc.indexOf(buggyChar) != -1) { doc = doc.replace(buggyChar, ' '); } ByteArrayInputStream in2 = new ByteArrayInputStream(doc.getBytes()); DOMParser parser = new DOMParser(); return parser.parseXML(in2); }
From source file:org.onecmdb.utils.internal.nmap.TransformNmap.java
public List<CiBean> transform() throws SAXException, IOException { // Parse discovery result DOMParser parser = new DOMParser(); Document doc = (Document) parser.parseXML(null); //parser.parse(inputFile); //Document doc = parser. NodeList hostList = doc.getElementsByTagName("host"); if (hostList == null) { return (Collections.EMPTY_LIST); }/*w w w . j av a 2s .c o m*/ List<CiBean> allBeans = new ArrayList<CiBean>(); for (int i = 0, j = 0; i < hostList.getLength(); i++) { System.out.println("${progress} " + i); Element host = (Element) hostList.item(i); List<CiBean> currentBeans = new ArrayList<CiBean>(); if (host != null) { Element status = (Element) host.getElementsByTagName("status").item(0); String state = status.getAttribute("state"); ValueBean stateValue = new ValueBean(); stateValue.setAlias("state"); stateValue.setValue(state); CiBean ipBean = new CiBean(); ipBean.setDerivedFrom(ipTemplate); ipBean.setTemplate(false); CiBean nicBean = new CiBean(); nicBean.setDerivedFrom(nicTemplate); nicBean.setTemplate(false); CiBean netIfBean = new CiBean(); netIfBean.setDerivedFrom(netIfTemplate); netIfBean.setTemplate(false); // Set state on NetIf. netIfBean.addAttributeValue(stateValue); String ipAddress = null; j++; NodeList addrList = host.getElementsByTagName("address"); for (int a = 0; a < addrList.getLength(); a++) { Element el = (Element) addrList.item(a); String addr = el.getAttribute("addr"); String type = el.getAttribute("addrtype"); if (type.equals("mac")) { nicBean.setAlias("mac-" + addr.replace(":", ".")); // Set mac address. nicBean.addAttributeValue(new ValueBean("mac", addr, false)); // Set vendor. String vendor = el.getAttribute("vendor"); nicBean.addAttributeValue(new ValueBean("vendor", vendor, false)); // Connect nicBean to netif. netIfBean.addAttributeValue(new ValueBean("nic", nicBean.getAlias(), true)); } else { ipBean.setDerivedFrom(ipTemplate); ipBean.setAlias("ip-" + addr); // Set ipAddress ipAddress = addr; ipBean.addAttributeValue(new ValueBean("ipAddress", addr, false)); // Set addr type ipBean.addAttributeValue(new ValueBean("addrType", type, false)); // Connect nicBean to netif. netIfBean.addAttributeValue(new ValueBean("ipAddress", ipBean.getAlias(), true)); } } netIfBean.setAlias("netif-" + ipBean.getAlias()); // Validate that the state is ok, since we retrive all ip's.. if (state.equals("down")) { if (beanProvider != null) { CiBean remote = beanProvider.getBean(netIfBean.getAlias()); if (remote == null) { continue; } } } currentBeans.add(ipBean); // Can be empty (loocal host) if (nicBean.getAlias() != null) { currentBeans.add(nicBean); } currentBeans.add(netIfBean); NodeList hostsNameList = host.getElementsByTagName("hostnames"); for (int hs = 0; hs < hostsNameList.getLength(); hs++) { Element hostnames = (Element) hostsNameList.item(hs); NodeList hostnameList = hostnames.getElementsByTagName("hostname"); for (int h = 0; h < hostnameList.getLength(); h++) { Element hostname = (Element) hostnameList.item(h); String name = hostname.getAttribute("name"); String type = hostname.getAttribute("type"); CiBean hostnameBean = new CiBean(); hostnameBean.setAlias("c-" + name); hostnameBean.setTemplate(false); hostnameBean.setDerivedFrom(hostnameTemplate); // Set hostname hostnameBean.addAttributeValue(new ValueBean("hostname", name, false)); // Connect this to a dnsEntry. CiBean dnsEntry = new CiBean(); dnsEntry.setTemplate(false); dnsEntry.setDerivedFrom(dnsEntryTemplate); dnsEntry.setAlias(name + "." + ipAddress); dnsEntry.addAttributeValue(new ValueBean("ip", ipBean.getAlias(), true)); dnsEntry.addAttributeValue(new ValueBean("hostname", hostnameBean.getAlias(), true)); dnsEntry.addAttributeValue(new ValueBean("type", type, false)); // Add it to the bean list. currentBeans.add(hostnameBean); currentBeans.add(dnsEntry); } } } allBeans.addAll(currentBeans); } return (allBeans); }