/** * Stax parser for a given stream handler and iso control chars set awarness to on. The iso * control chars in the xml file will be replaced by simple spaces, usefull for potentially bogus * XML files to parse, this has a small perfs overhead so use it only when necessary * * @param streamHandler the xml stream handler * @param isoControlCharsAwareParser true or false */ public StaxParser(XmlStreamHandler streamHandler, boolean isoControlCharsAwareParser) { this.streamHandler = streamHandler; XMLInputFactory xmlFactory = XMLInputFactory.newInstance(); if (xmlFactory instanceof WstxInputFactory) { WstxInputFactory wstxInputfactory = (WstxInputFactory) xmlFactory; wstxInputfactory.configureForLowMemUsage(); wstxInputfactory.getConfig().setUndeclaredEntityResolver(new UndeclaredEntitiesXMLResolver()); } xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, false); xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false); xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false); this.isoControlCharsAwareParser = isoControlCharsAwareParser; inf = new SMInputFactory(xmlFactory); }
/** * Creates a new instance of Index Of Model Mappings read from the corresponding file (Used for * persistence) */ public static void parseIndexFromFile() { // Read file and create the Index // check if file exists. If it exists open it and parse it. // Else return // File inFile = new File(Model3dIndex.getIndexPath() + "modelIndex.xml"); // error state check if (inFile.exists()) { try { Model3dIndex.getListofAllMetaEntries().clear(); FileReader tmpInReader = new FileReader(inFile); WstxInputFactory fin = new WstxInputFactory(); fin.configureForConvenience(); fin.setProperty( XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE); // <-- NEEDED TO GET ATTRIBUTES! // input XMLStreamReader2 sr = (XMLStreamReader2) fin.createXMLStreamReader(tmpInReader); SMInputCursor inputRootElement = SMInputFactory.rootElementCursor(sr); inputRootElement.getNext(); SMInputCursor childInElement = inputRootElement.childCursor(); String myText = ""; while (childInElement.getNext() != null) { if (!childInElement.getCurrEvent().hasText() && childInElement .getLocalName() .toLowerCase() .equals(Model3dIndex.getMetaEntryTag().toLowerCase())) { Model3dIndex.getListofAllMetaEntries().add(new Model3dIndexEntry(childInElement)); } } tmpInReader.close(); } catch (Exception e) { return; } } else return; }
public void testEmployee() throws Exception { System.setProperty("org.metatype.sxc.output.directory", "target/tmp-jaxb"); JAXBContext ctx = new JAXBContextImpl(new Class[] {Employee.class}); JAXBElement<Employee> jaxbE = (JAXBElement<Employee>) ctx.createUnmarshaller() .unmarshal( xif.createXMLStreamReader(getResourceAsStream("employee.xml")), Employee.class); assertNotNull(jaxbE); assertNotNull(jaxbE.getValue()); Employee e = jaxbE.getValue(); assertEquals("foo", e.getDivision()); assertEquals("bar", e.getName()); Marshaller marshaller = ctx.createMarshaller(); assertNotNull(marshaller); ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLStreamWriter writer = xof.createXMLStreamWriter(bos); try { marshaller.marshal(e, writer); fail("MarshalException should have been thrown!"); } catch (MarshalException ex) { } marshaller.marshal(jaxbE, writer); writer.close(); Document d = readDocument(bos.toByteArray()); addNamespace("i", "urn:xfire:inheritance"); assertValid("/i:in0/i:name[text()='bar']", d); assertValid("/i:in0/i:division[text()='foo']", d); }