Example usage for javax.xml.stream XMLInputFactory setProperty

List of usage examples for javax.xml.stream XMLInputFactory setProperty

Introduction

In this page you can find the example usage for javax.xml.stream XMLInputFactory setProperty.

Prototype

public abstract void setProperty(java.lang.String name, Object value) throws java.lang.IllegalArgumentException;

Source Link

Document

Allows the user to set specific feature/property on the underlying implementation.

Usage

From source file:EntityReferenceTest.java

public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);

    XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream("e.xml"));
    while (reader.hasNext()) {
        int event = reader.next();
        if (event == XMLStreamConstants.CHARACTERS)
            System.out.println(reader.getText());
        else if (event == XMLStreamConstants.ENTITY_REFERENCE) {
            System.out.println("en: " + reader.getLocalName());
            System.out.println("er: " + reader.getText());
        }// www.  j  a  v a 2 s .c o  m
    }
    inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);

    reader = inputFactory.createXMLStreamReader(new FileInputStream("e.xml"));
    while (reader.hasNext()) {
        int event = reader.next();
        if (event == XMLStreamConstants.CHARACTERS)
            System.out.println(reader.getText());
        else if (event == XMLStreamConstants.ENTITY_REFERENCE) {
            System.out.println("en: " + reader.getLocalName());
            System.out.println("er: " + reader.getText());
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    xmlif = XMLInputFactory.newInstance();
    xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
    xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);

    System.out.println("FACTORY: " + xmlif);
    System.out.println("filename = " + filename);

    FileInputStream fis = new FileInputStream(filename);

    XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new Main());

    int eventType = xmlr.getEventType();
    printEventType(eventType);//from  w  ww. j  a  va  2 s.c  o  m

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        printEventType(eventType);
        printName(xmlr, eventType);
        printText(xmlr);

        if (xmlr.isStartElement()) {
            printAttributes(xmlr);
        }
        printPIData(xmlr);
    }
}

From source file:MyStreamFilter.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    xmlif = XMLInputFactory.newInstance();
    xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
    xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);

    System.out.println("FACTORY: " + xmlif);
    System.out.println("filename = " + filename);

    FileInputStream fis = new FileInputStream(filename);

    XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new MyStreamFilter());

    int eventType = xmlr.getEventType();
    printEventType(eventType);/*www  .  j  av a2s .  com*/

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        printEventType(eventType);
        printName(xmlr, eventType);
        printText(xmlr);

        if (xmlr.isStartElement()) {
            printAttributes(xmlr);
        }
        printPIData(xmlr);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    xmlif = XMLInputFactory.newInstance();
    xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
    xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);

    System.out.println("FACTORY: " + xmlif);
    System.out.println("filename = " + filename);

    FileInputStream fis = new FileInputStream(filename);

    XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new MyFilter());

    int eventType = xmlr.getEventType();
    printEventType(eventType);//from   w  ww  . j  av a 2 s  . c o m

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        printEventType(eventType);
        printName(xmlr, eventType);
        printText(xmlr);

        if (xmlr.isStartElement()) {
            printAttributes(xmlr);
        }
        printPIData(xmlr);
    }
}

From source file:StaxCursorTest.java

public static void main(String[] args) throws Exception {

    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    xmlif = XMLInputFactory.newInstance();
    xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
    xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);

    try {/*  w w w.  ja  v  a2  s .c  om*/
        XMLStreamReader xmlr = xmlif.createXMLStreamReader(filename, new FileInputStream(filename));
        int eventType = xmlr.getEventType();
        printStartDocument(xmlr);
        while (xmlr.hasNext()) {
            eventType = xmlr.next();
            printStartElement(xmlr);
            printEndElement(xmlr);
            printText(xmlr);
            printPIData(xmlr);
            printComment(xmlr);
        }
    } catch (XMLStreamException ex) {
        System.out.println(ex.getMessage());
        if (ex.getNestedException() != null) {
            ex.getNestedException().printStackTrace();
        }
    }
}

From source file:Main.java

/**
 * Creates XMLInputFactory with DTD support disabled.
 * @return xml input factory//w ww .  java  2 s .  c  o m
 */
public static XMLInputFactory createBasicInputFactory() {
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    return xmlFactory;
}

From source file:Main.java

/**
 * Build a new XMLInputFactory.//from  ww  w. ja  v a 2 s. c  o m
 *
 * @return XML input factory
 */
public static XMLInputFactory getXmlInputFactory() {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    return inputFactory;
}

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");
    }//w  w w.  jav a2 s  .c om

    XMLInputFactory xif = XMLInputFactory.newFactory();
    xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
    xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    return xif.createXMLStreamReader(source);
}

From source file:eu.arthepsy.sonar.plugins.scapegoat.util.XmlUtils.java

public static SMInputFactory createFactory() {
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    SMInputFactory inputFactory = new SMInputFactory(xmlFactory);
    return inputFactory;
}

From source file:Main.java

/**
 * Creates an XMLInputFactory with unsafe features disabled. Such an XMLInputFactory is to be used for parsing
 * untrusted xml such as incoming post requests.
 * /*w  ww  .j  a  v a2  s  .  c om*/
 * @return
 */
public static XMLInputFactory newSafeInstance() {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();

    inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);

    return inputFactory;
}