List of usage examples for org.xml.sax InputSource InputSource
public InputSource(Reader characterStream)
From source file:DOMDump.java
static public void main(String[] arg) { boolean validate = true; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(validate);/* ww w.j a v a 2 s . c o m*/ dbf.setNamespaceAware(true); dbf.setIgnoringElementContentWhitespace(true); // Parse the input to produce a parse tree with its root // in the form of a Document object Document doc = null; try { DocumentBuilder builder = dbf.newDocumentBuilder(); builder.setErrorHandler(new MyErrorHandler()); InputSource is = new InputSource("personWithDTD.xml"); doc = builder.parse(is); } catch (SAXException e) { System.exit(1); } catch (ParserConfigurationException e) { System.err.println(e); System.exit(1); } catch (IOException e) { System.err.println(e); System.exit(1); } dump(doc); }
From source file:Main.java
static public void main(String[] arg) throws Exception { String filename = "yourXML.xml"; // Create a new factory that will create the parser. SAXParserFactory spf = SAXParserFactory.newInstance(); // Create the XMLReader to be used to parse the document. SAXParser parser = spf.newSAXParser(); XMLReader reader = parser.getXMLReader(); // Specify the error handler and the content handler. reader.setErrorHandler(new MyErrorHandler()); reader.setContentHandler(new MyContentHandler()); // Use the XMLReader to parse the entire file. InputSource is = new InputSource(filename); reader.parse(is);/* ww w . j a va2 s .c o m*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<?xml version='1.0' encoding='UTF-8'?>" + "<Employees>" + " <Employee emplid='1' type='admin'>" + " <firstname/>" + " <lastname>A</lastname>" + " <age>30</age>" + " <email>j@s.com</email>" + " </Employee>" + " <Employee emplid='2' type='admin'>" + " <firstname>S</firstname>" + " <lastname>H</lastname>" + " <age>32</age>" + " <email>s@h.com</email>" + " </Employee>" + "</Employees>"; List<String> ids = Arrays.asList("1", "2"); for (int i = 0; i < ids.size(); i++) { String employeeId = ids.get(i); String xpath = "/Employees/Employee[@emplid='" + employeeId + "']/firstname"; XPath xPath = XPathFactory.newInstance().newXPath(); String employeeFirstName = xPath.evaluate(xpath, new InputSource(new StringReader(xml))); if (employeeFirstName == "") { System.out.println("Employee " + employeeId + " has no first name."); } else {//from w ww . j a v a 2 s .c om System.out.println("Employee " + employeeId + "'s first name is " + employeeFirstName); } } }
From source file:Main.java
public static void main(String args[]) throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader reader = null;/* w w w . j av a2s .c o m*/ SAXParser parser = spf.newSAXParser(); reader = parser.getXMLReader(); reader.setErrorHandler(new MyErrorHandler()); reader.setContentHandler(new MyTextHandler()); StringReader sr = new StringReader( "<folks><person><phone>502 555-2192</phone><name>B, M</name></person></folks>"); //InputSource is = new InputSource("xmlFileName.xml"); InputSource is = new InputSource(sr); reader.parse(is); }
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 ww w .j a v a 2 s.c om*/ 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:DOMEdit.java
static public void main(String[] arg) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(true);//from w w w.j a v a 2s . c o m dbf.setNamespaceAware(true); dbf.setIgnoringElementContentWhitespace(true); Document doc = null; try { DocumentBuilder builder = dbf.newDocumentBuilder(); builder.setErrorHandler(new MyErrorHandler()); InputSource is = new InputSource("personWithDTD.xml"); doc = builder.parse(is); findByID(doc, "B"); write(doc); } catch (Exception e) { System.err.println(e); } }
From source file:MappingContentHandler.java
static public void main(String[] arg) { try {//from ww w . ja v a 2 s . c o m SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser parser = spf.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new MyErrorHandler()); MyTextHandler duper = new MyTextHandler(); reader.setContentHandler(duper); InputSource is = new InputSource("person.xml"); reader.parse(is); } catch (SAXException e) { System.out.println(e); } catch (ParserConfigurationException e) { System.err.println(e); System.exit(1); } catch (IOException e) { System.err.println(e); System.exit(1); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); findByID(doc, "uniqueID"); System.out.println(documentToString(doc)); }//w w w . j ava 2 s. c o m
From source file:MainClass.java
public static void main(String args[]) { try {//from w ww. j a v a2 s . c om SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean name = false; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("NAME")) { name = true; } } public void characters(char ch[], int start, int length) throws SAXException { if (name) { System.out.println("Name: " + new String(ch, start, length)); name = false; } } }; saxParser.parse(new InputSource(new StringReader(xmlString)), handler); } catch (Exception e) { e.printStackTrace(); } }
From source file:DOMEdit.java
static public void main(String[] arg) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(true);/*from ww w . j a v a2 s . c o m*/ dbf.setNamespaceAware(true); dbf.setIgnoringElementContentWhitespace(true); Document doc = null; try { DocumentBuilder builder = dbf.newDocumentBuilder(); builder.setErrorHandler(new MyErrorHandler()); InputSource is = new InputSource("personWithDTD.xml"); doc = builder.parse(is); insert(doc, "newName", "1111111111", "newEmail"); write(doc); } catch (Exception e) { System.err.println(e); } }