Example usage for javax.xml.stream XMLStreamConstants END_ELEMENT

List of usage examples for javax.xml.stream XMLStreamConstants END_ELEMENT

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamConstants END_ELEMENT.

Prototype

int END_ELEMENT

To view the source code for javax.xml.stream XMLStreamConstants END_ELEMENT.

Click Source Link

Document

Indicates an event is an end element

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    File source = new File(args[0]);
    File target = new File(source + ".new");

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    InputStream in = new FileInputStream(source);
    XMLEventReader reader = inputFactory.createXMLEventReader(in);

    OutputStream out = new FileOutputStream(target);
    XMLEventWriter writer = outputFactory.createXMLEventWriter(out);
    XMLEvent event;//from  ww w.j av a  2 s. c om

    boolean deleteSection = false;
    while (reader.hasNext()) {
        event = reader.nextEvent();
        if (event.getEventType() == XMLStreamConstants.START_ELEMENT
                && event.asStartElement().getName().toString().equalsIgnoreCase("companies")) {
            deleteSection = true;
            continue;
        } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT
                && (event.asEndElement().getName().toString().equalsIgnoreCase("companies"))) {
            deleteSection = false;
            continue;
        } else if (deleteSection) {
            continue;
        } else {
            writer.add(event);
        }
    }
}

From source file:Main.java

public static void skipElement(XMLStreamReader parser) throws XMLStreamException {
    int numEndTags = 1;
    while (numEndTags > 0) {
        switch (parser.next()) {
        case XMLStreamConstants.START_ELEMENT:
            ++numEndTags;//go down
            break;
        case XMLStreamConstants.END_ELEMENT:
            --numEndTags;//go up
            break;
        }// w  ww . j  a v a  2 s  . c  o  m
    }
}

From source file:Main.java

static protected void skip(XMLStreamReader parser) throws XMLStreamException {
    while (parser.hasNext()) {
        int eventType = parser.next();
        if (eventType == XMLStreamConstants.START_ELEMENT) {
            skip(parser);//w w  w.j ava 2s. c o  m
        } else if (eventType == XMLStreamConstants.END_ELEMENT) {
            return;
        }
    }
}

From source file:Main.java

public static String parseValue(XMLStreamReader xmlRdr, String elementName) throws XMLStreamException {
    String val = null;
    while (true) {
        xmlRdr.next();//  w  w w.  ja  v a  2s  .  com
        int event2 = xmlRdr.getEventType();
        if (event2 == XMLStreamConstants.CHARACTERS) {
            val = xmlRdr.getText();
        } else if (event2 == XMLStreamConstants.END_ELEMENT && xmlRdr.getLocalName().equals(elementName)) {
            return val;
        }
    }
}

From source file:Main.java

/**
 * Load text of the current element.//from w  ww  .  j av a  2  s. c o m
 * @param reader XML reader.
 * @return Text of the element.
 * @throws XMLStreamException when the text cannot be read or XML is malformed.
 */
public static String loadElementText(XMLStreamReader reader) throws XMLStreamException {
    reader.next();
    if (reader.isEndElement()) {
        return "";
    }
    reader.require(XMLStreamConstants.CHARACTERS, null, null);
    String text = reader.getText();
    reader.next();
    reader.require(XMLStreamConstants.END_ELEMENT, null, null);
    return text;
}

From source file:Main.java

public static boolean isEndElement(XMLStreamReader xmlRdr, String tagName) {
    return isElement(xmlRdr, XMLStreamConstants.END_ELEMENT, tagName);
}

From source file:Main.java

public static void readEndTag(XMLStreamReader reader, String localName) throws XMLStreamException {
    reader.nextTag();//from w  w w.  j a v a2 s .  c o m
    reader.require(XMLStreamConstants.END_ELEMENT, null, localName);
}

From source file:Main.java

/**
 * Advance the reader to the next end element and return true. Return false if next end element is not found
 * @param reader a XMLStreamReader/*from   w ww  .  j a v a 2  s. c om*/
 * @return True when successfully advanced the reader.
 * @throws XMLStreamException Exception when reading from the XMLStreamReader fails.
 */
public static boolean advanceToNextEndElement(XMLStreamReader reader) throws XMLStreamException {
    while (reader.getEventType() != XMLStreamConstants.END_ELEMENT) {
        if (reader.hasNext()) {
            reader.next();
        } else {
            // reach the end of elements in reader. Not found.
            return false;
        }
    }
    return true;
}

From source file:Main.java

public static String parse(XMLStreamReader reader) {
    StringBuffer result = new StringBuffer();
    if (reader != null) {
        try {//  w  w  w .j  a  va2 s. c  om
            while (reader.hasNext()) {
                switch (reader.getEventType()) {

                case XMLStreamConstants.START_ELEMENT:
                    result.append("<");
                    printName(reader, result);
                    printNamespaces(reader, result);
                    printAttributes(reader, result);
                    result.append(">");
                    break;

                case XMLStreamConstants.END_ELEMENT:
                    result.append("</");
                    printName(reader, result);
                    result.append(">");
                    break;

                case XMLStreamConstants.SPACE:

                case XMLStreamConstants.CHARACTERS:
                    int start = reader.getTextStart();
                    int length = reader.getTextLength();
                    result.append(new String(reader.getTextCharacters(), start, length));
                    break;

                case XMLStreamConstants.PROCESSING_INSTRUCTION:
                    result.append("<?");
                    if (reader.hasText())
                        result.append(reader.getText());
                    result.append("?>");
                    break;

                case XMLStreamConstants.CDATA:
                    result.append("<![CDATA[");
                    start = reader.getTextStart();
                    length = reader.getTextLength();
                    result.append(new String(reader.getTextCharacters(), start, length));
                    result.append("]]>");
                    break;

                case XMLStreamConstants.COMMENT:
                    result.append("<!--");
                    if (reader.hasText())
                        result.append(reader.getText());
                    result.append("-->");
                    break;

                case XMLStreamConstants.ENTITY_REFERENCE:
                    result.append(reader.getLocalName()).append("=");
                    if (reader.hasText())
                        result.append("[").append(reader.getText()).append("]");
                    break;

                case XMLStreamConstants.START_DOCUMENT:
                    result.append("<?xml");
                    result.append(" version='").append(reader.getVersion()).append("'");
                    result.append(" encoding='").append(reader.getCharacterEncodingScheme()).append("'");
                    if (reader.isStandalone())
                        result.append(" standalone='yes'");
                    else
                        result.append(" standalone='no'");
                    result.append("?>");
                    break;
                }
                reader.next();
            } // end while
        } catch (XMLStreamException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                reader.close();
            } catch (XMLStreamException e) {
            }
        }
    }
    return result.toString();
}

From source file:Main.java

public static void dumpXML(XMLStreamReader parser) throws XMLStreamException {
    int depth = 0;
    do {/*  w  w w. j  a va  2 s . c om*/
        switch (parser.getEventType()) {
        case XMLStreamConstants.START_ELEMENT:
            for (int i = 1; i < depth; ++i) {
                System.out.print("    ");
            }
            System.out.print("<");
            System.out.print(parser.getLocalName());
            for (int i = 0; i < parser.getAttributeCount(); ++i) {
                System.out.print(" ");
                System.out.print(parser.getAttributeLocalName(i));
                System.out.print("=\"");
                System.out.print(parser.getAttributeValue(i));
                System.out.print("\"");
            }
            System.out.println(">");

            ++depth;
            break;
        case XMLStreamConstants.END_ELEMENT:
            --depth;
            for (int i = 1; i < depth; ++i) {
                System.out.print("    ");
            }
            System.out.print("</");
            System.out.print(parser.getLocalName());
            System.out.println(">");
            break;
        case XMLStreamConstants.CHARACTERS:
            for (int i = 1; i < depth; ++i) {
                System.out.print("    ");
            }

            System.out.println(parser.getText());
            break;
        }

        if (depth > 0)
            parser.next();
    } while (depth > 0);
}