List of usage examples for org.w3c.dom Element getAttribute
public String getAttribute(String name);
From source file:Main.java
public static void main(String[] args) throws Exception { Main example = new Main(); example.data.put("France", "Paris"); example.data.put("Japan", "Tokyo"); JAXBContext context = JAXBContext.newInstance(Main.class); Marshaller marshaller = context.createMarshaller(); DOMResult result = new DOMResult(); marshaller.marshal(example, result); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); Document document = (Document) result.getNode(); XPathExpression expression = xpath.compile("//map/entry"); NodeList nodes = (NodeList) expression.evaluate(document, XPathConstants.NODESET); expression = xpath.compile("//map"); Node oldMap = (Node) expression.evaluate(document, XPathConstants.NODE); Element newMap = document.createElement("map"); for (int index = 0; index < nodes.getLength(); index++) { Element element = (Element) nodes.item(index); newMap.setAttribute(element.getAttribute("key"), element.getAttribute("value")); }/*from ww w. j a v a 2s . c o m*/ expression = xpath.compile("//map/.."); Node parent = (Node) expression.evaluate(document, XPathConstants.NODE); parent.replaceChild(newMap, oldMap); TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(System.out)); }
From source file:Main.java
public static void main(String[] args) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader("tds.xml")), XPathConstants.NODESET); for (int i = 0; i < shows.getLength(); i++) { Element show = (Element) shows.item(i); String guestName = xPath.evaluate("guest/name", show); System.out.println(guestName); System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date")); }/*from w w w. j a v a 2 s . c o m*/ }
From source file:GuestList.java
public static void main(String[] args) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader("tds.xml")), XPathConstants.NODESET); for (int i = 0; i < shows.getLength(); i++) { Element show = (Element) shows.item(i); String guestName = xPath.evaluate("guest/name", show); String guestCredit = xPath.evaluate("guest/credit", show); System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date") + " - " + guestName + " (" + guestCredit + ")"); }/*from w w w .ja v a2 s .com*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<Services><service name='qwerty' id=''><File rootProfile='abcd' extension='acd'><Columns>" + "<name id='0' profileName='DATE' type='java'></name><name id='1' profileName='DATE' type='java'></name>" + "</Columns></File><File rootProfile='efg' extension='ghi'><Columns><name id='a' profileName='DATE' type='java'></name>" + "<name id='b' profileName='DATE' type='java'></name></Columns></File></service></Services>"; DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = null; documentBuilder = documentFactory.newDocumentBuilder(); org.w3c.dom.Document doc = documentBuilder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes()))); doc.getDocumentElement().normalize(); NodeList nodeList0 = doc.getElementsByTagName("service"); NodeList nodeList1 = null;/*from w ww . j av a 2s . c o m*/ NodeList nodeList2 = null; System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); for (int temp0 = 0; temp0 < nodeList0.getLength(); temp0++) { Node node0 = nodeList0.item(temp0); System.out.println("\nElement type :" + node0.getNodeName()); Element Service = (Element) node0; if (node0.getNodeType() != Node.ELEMENT_NODE) { continue; } System.out.println("name : " + Service.getAttribute("name")); System.out.println("id : " + Service.getAttribute("id")); nodeList1 = Service.getChildNodes(); for (int temp = 0; temp < nodeList1.getLength(); temp++) { Node node1 = nodeList1.item(temp); System.out.println("\nElement type :" + node1.getNodeName()); Element File = (Element) node1; if (node1.getNodeType() != Node.ELEMENT_NODE) { continue; } System.out.println("rootProfile:" + File.getAttribute("rootProfile")); System.out.println("extension : " + File.getAttribute("extension")); nodeList2 = File.getChildNodes();// colums for (int temp1 = 0; temp1 < nodeList2.getLength(); temp1++) { Element column = (Element) nodeList2.item(temp1); NodeList nodeList4 = column.getChildNodes(); for (int temp3 = 0; temp3 < nodeList4.getLength(); temp3++) { Element name = (Element) nodeList4.item(temp3); if (name.getNodeType() != Node.ELEMENT_NODE) { continue; } System.out.println("id:" + name.getAttribute("id")); System.out.println("profileName : " + name.getAttribute("profileName")); System.out.println("type : " + name.getAttribute("type")); } } } } }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);// ww w .jav a 2 s. c om factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); element.setAttribute("newAttrName", "attrValue"); element.setAttribute("newAttrName", "<>&\"'"); element.removeAttribute("value"); boolean has = element.hasAttribute("value"); // true String attrValue = element.getAttribute("value"); // mydefault }
From source file:com.twinsoft.convertigo.beans.CheckBeans.java
public static void main(String[] args) { Engine.logBeans = Logger.getLogger(BeansDoc.class); srcBase = args[0];// w w w.j av a2s .c o m System.out.println("Loading database objects XML DB in " + srcBase); try { defaultDocumentBuilderFactory = DocumentBuilderFactory.newInstance(); defaultDocumentBuilder = defaultDocumentBuilderFactory.newDocumentBuilder(); dboXmlDeclaredDatabaseObjects = new ArrayList<String>(); Document dboXmlDocument = defaultDocumentBuilder.parse( new FileInputStream(new File(srcBase + "/com/twinsoft/convertigo/beans/database_objects.xml"))); NodeList beanList = dboXmlDocument.getElementsByTagName("bean"); int n = beanList.getLength(); for (int i = 0; i < n; i++) { Element element = (Element) beanList.item(i); dboXmlDeclaredDatabaseObjects.add(element.getAttribute("classname")); } } catch (Exception e) { System.out.println("Error while loading DBO XML DB"); e.printStackTrace(); System.exit(-1); } System.out.println("Browsing sources in " + srcBase); browsePackages(srcBase + "com/twinsoft/convertigo/beans"); System.out.println(); System.out.println("Found " + javaClassNames.size() + " classes"); for (String javaClassName : javaClassNames) { analyzeJavaClass(javaClassName); } for (String icon : icons) { Error.BEAN_ICON_NOT_USED.add(icon); } System.out.println(); for (Error error : Error.values()) { List<String> errorList = errors.get(error); if (errorList == null) continue; int nError = errorList.size(); System.out.println(error + " (" + nError + ")"); for (String errorMessage : errorList) { System.out.println(" " + errorMessage); } System.out.println(); } System.out.println(); System.out.println("======="); System.out.println("Summary"); System.out.println("======="); System.out.println(); System.out.println("Found " + nBeanClass + " bean classes (including abstract classes)"); System.out.println("Found " + nBeanClassNotAbstract + " instantiable bean classes"); System.out.println(); int nTotalError = 0; for (Error error : Error.values()) { List<String> errorList = errors.get(error); int nError = 0; if (errorList != null) nError = errorList.size(); nTotalError += nError; System.out.println(error + ": found " + nError + " error(s)"); } System.out.println(); System.out.println("Found " + nTotalError + " error(s)"); System.out.println(); System.out.println("Beans check finished!"); System.exit(nTotalError); }
From source file:ValidateLicenseHeaders.java
/** * ValidateLicenseHeaders jboss-src-root * /*from w w w . j ava2 s. c o m*/ * @param args */ public static void main(String[] args) throws Exception { if (args.length == 0 || args[0].startsWith("-h")) { log.info("Usage: ValidateLicenseHeaders [-addheader] jboss-src-root"); System.exit(1); } int rootArg = 0; if (args.length == 2) { if (args[0].startsWith("-add")) addDefaultHeader = true; else { log.severe("Uknown argument: " + args[0]); log.info("Usage: ValidateLicenseHeaders [-addheader] jboss-src-root"); System.exit(1); } rootArg = 1; } File jbossSrcRoot = new File(args[rootArg]); if (jbossSrcRoot.exists() == false) { log.info("Src root does not exist, check " + jbossSrcRoot.getAbsolutePath()); System.exit(1); } URL u = Thread.currentThread().getContextClassLoader() .getResource("META-INF/services/javax.xml.parsers.DocumentBuilderFactory"); System.err.println(u); // Load the valid copyright statements for the licenses File licenseInfo = new File(jbossSrcRoot, "varia/src/etc/license-info.xml"); if (licenseInfo.exists() == false) { log.severe("Failed to find the varia/src/etc/license-info.xml under the src root"); System.exit(1); } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); Document doc = db.parse(licenseInfo); NodeList licenses = doc.getElementsByTagName("license"); for (int i = 0; i < licenses.getLength(); i++) { Element license = (Element) licenses.item(i); String key = license.getAttribute("id"); ArrayList headers = new ArrayList(); licenseHeaders.put(key, headers); NodeList copyrights = license.getElementsByTagName("terms-header"); for (int j = 0; j < copyrights.getLength(); j++) { Element copyright = (Element) copyrights.item(j); copyright.normalize(); String id = copyright.getAttribute("id"); // The id will be blank if there is no id attribute if (id.length() == 0) continue; String text = getElementContent(copyright); if (text == null) continue; // Replace all duplicate whitespace and '*' with a single space text = text.replaceAll("[\\s*]+", " "); if (text.length() == 1) continue; text = text.toLowerCase().trim(); // Replace any copyright date0-date1,date2 with copyright ... text = text.replaceAll(COPYRIGHT_REGEX, "..."); LicenseHeader lh = new LicenseHeader(id, text); headers.add(lh); } } log.fine(licenseHeaders.toString()); File[] files = jbossSrcRoot.listFiles(dotJavaFilter); log.info("Root files count: " + files.length); processSourceFiles(files, 0); log.info("Processed " + totalCount); log.info("Updated jboss headers: " + jbossCount); // Files with no headers details log.info("Files with no headers: " + noheaders.size()); FileWriter fw = new FileWriter("NoHeaders.txt"); for (Iterator iter = noheaders.iterator(); iter.hasNext();) { File f = (File) iter.next(); fw.write(f.getAbsolutePath()); fw.write('\n'); } fw.close(); // Files with unknown headers details log.info("Files with invalid headers: " + invalidheaders.size()); fw = new FileWriter("InvalidHeaders.txt"); for (Iterator iter = invalidheaders.iterator(); iter.hasNext();) { File f = (File) iter.next(); fw.write(f.getAbsolutePath()); fw.write('\n'); } fw.close(); // License usage summary log.info("Creating HeadersSummary.txt"); fw = new FileWriter("HeadersSummary.txt"); for (Iterator iter = licenseHeaders.entrySet().iterator(); iter.hasNext();) { Map.Entry entry = (Map.Entry) iter.next(); String key = (String) entry.getKey(); fw.write("+++ License type=" + key); fw.write('\n'); List list = (List) entry.getValue(); Iterator jiter = list.iterator(); while (jiter.hasNext()) { LicenseHeader lh = (LicenseHeader) jiter.next(); fw.write('\t'); fw.write(lh.id); fw.write(", count="); fw.write("" + lh.count); fw.write('\n'); } } fw.close(); }
From source file:Main.java
public static String getDomAttributeValue(String attrib, Element element) { return element.getAttribute(attrib); }
From source file:Main.java
public static String getElementId(Element element) { return element.getAttribute("id"); }
From source file:Main.java
public static String getAttribute(Element e, String name) { return e.getAttribute(name); }