List of usage examples for javax.xml.stream XMLInputFactory newFactory
public static XMLInputFactory newFactory() throws FactoryConfigurationError
From source file:Main.java
public static void main(String[] args) throws Exception { XMLInputFactory xif = XMLInputFactory.newFactory(); StreamSource xmlSource = new StreamSource("src/forum19559825/input.xml"); XMLStreamReader xsr = xif.createXMLStreamReader(xmlSource); positionXMLStreamReaderAtAnyElement(xsr); processAnyElement(xsr);/* w ww .j ava 2 s. c o m*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("data.xml")); xsr.nextTag(); // advance to Employees tag xsr.nextTag(); // advance to first Employer element Map<String, String> map = new HashMap<String, String>(); while (xsr.getLocalName().equals("Employee")) { map.put(xsr.getAttributeValue("", "id"), xsr.getElementText()); xsr.nextTag(); // advance to next Employer element }// w ww .j ava 2 s.c o m }
From source file:Main.java
public static void main(String[] args) throws Exception { XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory(); XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new FileInputStream("input.xml")); InterestingElementFilter filter = new InterestingElementFilter(); XMLEventReader interestingElementReader = xmlInputFactory.createFilteredReader(xmlEventReader, filter); while (interestingElementReader.hasNext()) { XMLEvent xmlEvent = interestingElementReader.peek(); if (xmlEvent.isStartElement()) { System.out.println(xmlEvent.asStartElement().getName()); }//ww w.ja v a 2 s . c o m interestingElementReader.next(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { XMLInputFactory xif = XMLInputFactory.newFactory(); FileInputStream xml = new FileInputStream("input.xml"); XMLStreamReader xsr = xif.createXMLStreamReader(xml); xsr.nextTag(); // Advance to "Persons" tag xsr.nextTag(); // Advance to "Person" tag JAXBContext jc = JAXBContext.newInstance(Person.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); List<Person> persons = new ArrayList<Person>(); while (xsr.hasNext() && xsr.isStartElement()) { Person person = (Person) unmarshaller.unmarshal(xsr); persons.add(person);// w w w . j av a 2 s .c o m xsr.nextTag(); } for (Person person : persons) { System.out.println(person.getName()); } }
From source file:Main.java
public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Customer.class); XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(new File("input.xml"))); Unmarshaller unmarshaller = jc.createUnmarshaller(); unmarshaller.setListener(new LocationListener(xsr)); Customer customer = (Customer) unmarshaller.unmarshal(xsr); }
From source file:Main.java
public static void main(String[] args) throws Exception { XMLOutputFactory xof = XMLOutputFactory.newFactory(); StringWriter sw = new StringWriter(); XMLStreamWriter xsw = xof.createXMLStreamWriter(sw); xsw.writeStartDocument();/* w w w .ja v a2 s . co m*/ xsw.writeStartElement("foo"); xsw.writeCharacters("<>\"&'"); xsw.writeEndDocument(); String xml = sw.toString(); System.out.println(xml); // READ THE XML XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml)); xsr.nextTag(); // Advance to "foo" element System.out.println(xsr.getElementText()); }
From source file:Main.java
public static Object xmlStrToObj(String inputStr, Class inputClass) throws Exception { byte[] byteArray = inputStr.getBytes(); ByteArrayInputStream byteStream = new ByteArrayInputStream(byteArray); XMLInputFactory input = XMLInputFactory.newFactory(); XMLStreamReader reader = input.createXMLStreamReader(byteStream); JAXBContext context = JAXBContext.newInstance(inputClass); Unmarshaller unmarsh = context.createUnmarshaller(); Object result = unmarsh.unmarshal(reader); return result; }
From source file:Main.java
public static String digest(InputStream in) throws Exception { MessageDigest messageDigest = null; XMLInputFactory inputFactory = XMLInputFactory.newFactory(); messageDigest = MessageDigest.getInstance("MD5"); XMLEventReader eventReader = inputFactory.createXMLEventReader(in); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { messageDigest.update(event.asStartElement().getName().toString().getBytes()); } else if (event.isEndElement()) { messageDigest.update(event.asEndElement().getName().toString().getBytes()); }/*from w w w . ja va 2 s .c om*/ } StringBuffer result = new StringBuffer(); byte[] digest = messageDigest.digest(); for (byte b : digest) { result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1)); } return result.toString(); }
From source file:Main.java
/** * Converts the XML file specified into the specified POJO type * @param <T> the object type of the POJO * @param xmlfile the XML file to convert * @param classOfT the class of the POJO * @return the POJO object if conversion was successful * @throws JAXBException/*from w w w. ja v a2 s . c om*/ * @throws XMLStreamException * @throws FileNotFoundException */ public static <T> T convertToPojo(File xmlfile, Class<T> classOfT) throws JAXBException, XMLStreamException, FileNotFoundException { JAXBContext jaxbContext = JAXBContext.newInstance(classOfT); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); XMLInputFactory xif = XMLInputFactory.newFactory(); // settings to prevent xxe // would be funny if this tool is itsef is vulnerable to xxe :D xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader(xmlfile)); T t = (T) jaxbUnmarshaller.unmarshal(xsr);//(xmlfile); return t; }
From source file:Main.java
public static XMLStreamReader createSafeReader(StreamSource source) throws XMLStreamException { if (source == null) { throw new IllegalArgumentException("The provided source cannot be null"); }/*from www.j av a 2s . c o m*/ XMLInputFactory xif = XMLInputFactory.newFactory(); xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); return xif.createXMLStreamReader(source); }