List of usage examples for org.w3c.dom Document getDocumentElement
public Element getDocumentElement();
From source file:Main.java
private static Document pruneDocument(Document doc, int maxElement) throws ParserConfigurationException { if (maxElement == -1) { return doc; }/* w ww .jav a 2s.c o m*/ Document newDoc = (Document) doc.cloneNode(false); Element newRoot = (Element) doc.getDocumentElement().cloneNode(false); newDoc.adoptNode(newRoot); newDoc.appendChild(newRoot); NodeList nl = doc.getDocumentElement().getChildNodes(); for (int i = 0; i < maxElement && i < nl.getLength(); i++) { if (!(nl.item(i) instanceof Element)) { maxElement++; continue; } Node item = nl.item(i).cloneNode(true); newDoc.adoptNode(item); newDoc.getDocumentElement().appendChild(item); } if (debug) System.out.println("Creating document of " + newDoc.getDocumentElement().getChildNodes().getLength()); return newDoc; }
From source file:Main.java
public static void testx() { try {//from ww w . j a va 2 s . co m File fXmlFile = new File("C:\\Users\\is96092\\Desktop\\music\\Megaman2.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); //optional, but recommended //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("staff"); System.out.println("----------------------------"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); System.out.println("\nCurrent Element :" + nNode.getNodeName()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("Staff id : " + eElement.getAttribute("id")); System.out.println( "First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent()); System.out.println( "Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent()); System.out.println( "Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent()); System.out.println( "Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent()); } } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.cloud.test.utils.UtilsForTest.java
public static ArrayList<HashMap<String, String>> parseMulXML(InputStream is, String[] tagNames) { ArrayList<HashMap<String, String>> returnValues = new ArrayList<HashMap<String, String>>(); try {//from w w w .ja va2s .c o m DocumentBuilder docBuilder = factory.newDocumentBuilder(); Document doc = docBuilder.parse(is); Element rootElement = doc.getDocumentElement(); for (int i = 0; i < tagNames.length; i++) { NodeList targetNodes = rootElement.getElementsByTagName(tagNames[i]); if (targetNodes.getLength() <= 0) { System.out.println("no " + tagNames[i] + " tag in XML response...returning null"); } else { for (int j = 0; j < targetNodes.getLength(); j++) { HashMap<String, String> valueList = new HashMap<String, String>(); Node node = targetNodes.item(j); //parse child nodes NodeList child = node.getChildNodes(); for (int c = 0; c < node.getChildNodes().getLength(); c++) { child.item(c).getNodeName(); valueList.put(child.item(c).getNodeName(), child.item(c).getTextContent()); } returnValues.add(valueList); } } } } catch (Exception ex) { System.out.println(ex); } return returnValues; }
From source file:Main.java
private static NodeList getNodeList(String xmlContent, String expression) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException { Document document = parse(xmlContent, CHARSET_UTF8); XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xpath.evaluate(expression, document.getDocumentElement(), XPathConstants.NODESET); return nodeList; }
From source file:ac.uk.diamond.sample.HttpClientTest.Utils.java
/** * Helper method that deserializes and unmarshalls the message from the given stream. This * method has been adapted from {@code org.opensaml.ws.message.decoder.BaseMessageDecoder}. * /*from w ww . ja v a 2 s . c o m*/ * @param messageStream * input stream containing the message * * @return the inbound message * * @throws MessageDecodingException * thrown if there is a problem deserializing and unmarshalling the message */ static XMLObject unmarshallMessage(ParserPool parserPool, String messageStream) throws ClientProtocolException { try { Document messageDoc = parserPool.parse(new StringReader(messageStream)); Element messageElem = messageDoc.getDocumentElement(); Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(messageElem); if (unmarshaller == null) { throw new ClientProtocolException( "Unable to unmarshall message, no unmarshaller registered for message element " + XMLHelper.getNodeQName(messageElem)); } XMLObject message = unmarshaller.unmarshall(messageElem); return message; } catch (XMLParserException e) { throw new ClientProtocolException("Encountered error parsing message into its DOM representation", e); } catch (UnmarshallingException e) { throw new ClientProtocolException("Encountered error unmarshalling message from its DOM representation", e); } }
From source file:com.spun.util.ups.UPSUtils.java
/***********************************************************************/ private static UPSQuote[] extractQuotes(InputStream response) throws SAXException, IOException, ParserConfigurationException, FactoryConfigurationError { Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(response); ArrayList<UPSQuote> quotes = new ArrayList<UPSQuote>(); NodeList list = document.getDocumentElement().getElementsByTagName("RatedShipment"); for (int i = 0; i < list.getLength(); i++) { quotes.add(extractQuote(list.item(i))); }/* w w w .jav a 2 s . c o m*/ if (quotes.size() == 0) { MySystem.warning("Couldn't find quote in response " + getDocument(document)); } return UPSQuote.toArray(quotes); }
From source file:com.sun.portal.portletcontainer.admin.PortletRegistryHelper.java
public static Element getRootElement(Document document) { if (document != null) { return document.getDocumentElement(); }/* w w w . j a v a 2 s. co m*/ return null; }
From source file:eu.planets_project.services.utils.cli.CliMigrationPaths.java
/** * @param resourceName The XML file containing the path configuration * @return The paths defined in the XML file * @throws ParserConfigurationException/*w w w .j a v a2 s .c o m*/ * @throws IOException * @throws SAXException * @throws URISyntaxException */ // TODO: Clean up this exception-mess. Something like "InitialisationException" or "ConfigurationErrorException" should cover all these exceptions. public static CliMigrationPaths initialiseFromFile(String resourceName) throws ParserConfigurationException, IOException, SAXException, URISyntaxException { InputStream instream; ClassLoader loader = Thread.currentThread().getContextClassLoader(); URL resourceURL = loader.getResource(resourceName); if (new File(defaultPath, resourceName).isFile()) { instream = new FileInputStream(new File(defaultPath, resourceName)); } else if (new File(resourceName).isFile()) { instream = new FileInputStream(new File(resourceName)); } else if (resourceURL != null) { instream = resourceURL.openStream(); } else { String msg = String.format("Could not locate resource %s", resourceName); throw new FileNotFoundException(msg); } CliMigrationPaths paths = new CliMigrationPaths(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); Document doc = builder.parse(instream); Element fileformats = doc.getDocumentElement(); if (fileformats != null) { NodeList children = fileformats.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeName().equals("path")) { CliMigrationPath pathdef = decodePathNode(child); paths.add(pathdef); } } } } IOUtils.closeQuietly(instream); return paths; }
From source file:Main.java
public static void ReadXMLFile() { try {/*from ww w. j av a 2 s. c o m*/ File fXmlFile = new File("D:\\FAR_Documents\\__Startamap\\Home.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); //optional, but recommended //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("staff"); System.out.println("----------------------------"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); System.out.println("\nCurrent Element :" + nNode.getNodeName()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("Staff id : " + eElement.getAttribute("id")); System.out.println( "First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent()); System.out.println( "Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent()); System.out.println( "Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent()); System.out.println( "Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent()); } } } catch (Exception e) { e.printStackTrace(); } }
From source file:de.betterform.session.DefaultSerializer.java
/** * @param document// ww w. ja va2s. c o m * @return */ private static NodeWrapper getDocumentElementContext(Document document) { return new DocumentWrapper(document, "configuration.xml", new IndependentContext().getConfiguration()) .wrap(document.getDocumentElement()); }