List of usage examples for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder
public abstract DocumentBuilder newDocumentBuilder() throws ParserConfigurationException;
From source file:Main.java
static public void main(String[] arg) { boolean validate = true; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(validate);//from ww w .j a v a2 s . co m dbf.setNamespaceAware(true); try { DocumentBuilder builder = dbf.newDocumentBuilder(); builder.setErrorHandler(new MyErrorHandler()); InputSource is = new InputSource(new StringReader(getXMLData())); Document doc = builder.parse(is); } catch (SAXException e) { System.out.println(e); } catch (ParserConfigurationException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } }
From source file:interoperabilite.webservice.fluent.FluentResponseHandling.java
public static void main(String[] args) throws Exception { Document result = Request.Get("http://somehost/content").execute() .handleResponse(new ResponseHandler<Document>() { @Override//from w ww. j av a 2 s .c o m public Document handleResponse(final HttpResponse response) throws IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } if (entity == null) { throw new ClientProtocolException("Response contains no content"); } DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); ContentType contentType = ContentType.getOrDefault(entity); if (!contentType.equals(ContentType.APPLICATION_XML)) { throw new ClientProtocolException("Unexpected content type:" + contentType); } Charset charset = contentType.getCharset(); if (charset == null) { charset = Consts.ISO_8859_1; } return docBuilder.parse(entity.getContent(), charset.name()); } catch (ParserConfigurationException ex) { throw new IllegalStateException(ex); } catch (SAXException ex) { throw new ClientProtocolException("Malformed XML document", ex); } } }); // Do something useful with the result System.out.println(result); }
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;// ww w . ja v a2 s. com 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
static public void main(String[] arg) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(true);//from w w w. j av a2s . com dbf.setNamespaceAware(true); dbf.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = dbf.newDocumentBuilder(); StringReader sr = new StringReader("<tag>java2s.com</tag>"); Document document = builder.parse(new InputSource(sr)); deleteFirstElement(document); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); StringWriter sw = new StringWriter(); trans.transform(new DOMSource(document), new StreamResult(sw)); System.out.println(sw.toString()); }
From source file:Main.java
public static void main(String[] args) throws Exception { File CFile = new File("data.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true);// w w w . j a v a 2 s .c o m factory.setIgnoringElementContentWhitespace(true); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(CFile); NodeList pizzas = document.getElementsByTagName("Pizza"); for (int i = 0; i < pizzas.getLength(); i++) { Element pizzaSize = (Element) pizzas.item(i); String pSize = pizzaSize.getAttribute("Size"); if (pSize.equalsIgnoreCase("Large")) System.out.println(10.0); if (pSize.equalsIgnoreCase("Medium")) System.out.println(7.0); if (pSize.equalsIgnoreCase("Small")) System.out.println(5.0); } }
From source file:io.aos.protocol.http.httpcommon.FluentResponseHandling.java
public static void main(String... args) throws Exception { Document result = Request.Get("http://somehost/content").execute() .handleResponse(new ResponseHandler<Document>() { public Document handleResponse(final HttpResponse response) throws IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); }/* w w w. j a v a 2 s . com*/ if (entity == null) { throw new ClientProtocolException("Response contains no content"); } DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); ContentType contentType = ContentType.getOrDefault(entity); if (!contentType.equals(ContentType.APPLICATION_XML)) { throw new ClientProtocolException("Unexpected content type:" + contentType); } Charset charset = contentType.getCharset(); if (charset == null) { charset = HTTP.DEF_CONTENT_CHARSET; } return docBuilder.parse(entity.getContent(), charset.name()); } catch (ParserConfigurationException ex) { throw new IllegalStateException(ex); } catch (SAXException ex) { throw new ClientProtocolException("Malformed XML document", ex); } } }); // Do something useful with the result System.out.println(result); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/* ww w .ja v a2 s .c o m*/ // Prevent expansion of entity references factory.setExpandEntityReferences(false); // Create the builder and parse the file Document doc = factory.newDocumentBuilder().parse(new File("filename")); }
From source file:edu.ucsb.cs.eager.sa.cerebro.ProfessorX.java
public static void main(String[] args) { Options options = new Options(); options.addOption("i", "input-file", true, "Path to input xml file"); options.addOption("r", "root-path", true, "Root path of all Git repositories"); CommandLine cmd;//from w w w . j av a 2 s . com try { CommandLineParser parser = new BasicParser(); cmd = parser.parse(options, args); } catch (ParseException e) { System.err.println("Error: " + e.getMessage() + "\n"); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("Cerebro", options); return; } String inputFileName = cmd.getOptionValue("i"); if (inputFileName == null) { System.err.println("input file path is required"); return; } String rootPath = cmd.getOptionValue("r"); if (rootPath == null) { System.err.println("root path is required"); return; } File inputFile = new File(inputFileName); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); Document doc; try { DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); doc = dBuilder.parse(inputFile); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); return; } NodeList repoList = doc.getElementsByTagName("repo"); for (int i = 0; i < repoList.getLength(); i++) { Element repo = (Element) repoList.item(i); String name = repo.getElementsByTagName("name").item(0).getTextContent(); String classPath = repo.getElementsByTagName("classpath").item(0).getTextContent(); Set<String> classes = new LinkedHashSet<String>(); NodeList classesList = repo.getElementsByTagName("classes").item(0).getChildNodes(); for (int j = 0; j < classesList.getLength(); j++) { if (!(classesList.item(j) instanceof Element)) { continue; } classes.add(classesList.item(j).getTextContent()); } analyzeRepo(rootPath, name, classPath, classes); } }
From source file:ListMoviesXML.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true);//from ww w . ja v a 2 s .com factory.setIgnoringElementContentWhitespace(true); factory.setValidating(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource("y.xml")); Element root = doc.getDocumentElement(); Element movieElement = (Element) root.getFirstChild(); Movie m; while (movieElement != null) { m = getMovie(movieElement); String msg = Integer.toString(m.year); msg += ": " + m.title; msg += " (" + m.price + ")"; System.out.println(msg); movieElement = (Element) movieElement.getNextSibling(); } }
From source file:MainClass.java
public static void main(String args[]) throws Exception { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); builderFactory.setValidating(true);/* w ww . ja va 2 s.com*/ DocumentBuilder builder = null; builder = builderFactory.newDocumentBuilder(); builder.setErrorHandler(new MainClass()); Document xmlDoc = null; xmlDoc = builder.parse(new File("y.xml")); listNodes(xmlDoc); }