Java XMLStreamReader readEvent(XMLStreamReader reader)

Here you can find the source of readEvent(XMLStreamReader reader)

Description

read Event

License

Open Source License

Declaration

public static String readEvent(XMLStreamReader reader) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.xml.stream.XMLStreamConstants;

import javax.xml.stream.XMLStreamReader;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class Main {
    public static String readEvent(XMLStreamReader reader) {
        StringBuffer buf = new StringBuffer();
        int type = reader.getEventType();
        switch (type) {
        case XMLStreamConstants.START_DOCUMENT:
            break;
        case XMLStreamConstants.END_DOCUMENT:
            break;
        case XMLStreamConstants.START_ELEMENT:
            buf.append('<');
            if (reader.getPrefix() != null && reader.getPrefix() != "") {
                buf.append(reader.getPrefix() + ":");
            }//from w  ww  .  j a v  a  2  s .c  o m
            buf.append(reader.getLocalName());
            for (int i = 0; i < reader.getNamespaceCount(); i++) {
                buf.append(" xmlns:" + reader.getNamespacePrefix(i) + "=\"" + reader.getNamespaceURI(i) + '\"');
            }
            for (int i = 0; i < reader.getAttributeCount(); i++) {
                buf.append(' ');
                if (reader.getAttributePrefix(i) != null && reader.getAttributePrefix(i) != "") {
                    buf.append(reader.getAttributePrefix(i) + ":");
                }
                buf.append(reader.getAttributeLocalName(i) + "=\"" + reader.getAttributeValue(i) + '\"');
            }
            buf.append('>');
            break;
        case XMLStreamConstants.END_ELEMENT:
            buf.append("</");
            if (reader.getPrefix() != null && reader.getPrefix() != "") {
                buf.append(reader.getPrefix() + ":");
            }
            buf.append(reader.getLocalName() + '>');
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            break;
        case XMLStreamConstants.CHARACTERS:
            buf.append(reader.getText());
            break;
        case XMLStreamConstants.COMMENT:
            break;
        }
        return buf.toString();
    }

    /**
     * returns the concatenation of all text children within the node. Does not
     * recurse into subelements.
     */
    public static String getText(Element config) {
        if (config == null)
            return new String("");
        NodeList children = config.getChildNodes();
        Node node;
        String out = "";
        for (int i = 0; i < children.getLength(); i++) {
            node = children.item(i);
            if (node instanceof Text) {
                out += node.getNodeValue();
            }
        }
        return out;
    }
}

Related

  1. printEvent(XMLStreamReader xmlr)
  2. printEvent(XMLStreamReader xmlr, boolean showEvents)
  3. printLiterally(final PrintStream os, final XMLStreamReader xmlReader)
  4. printText(XMLStreamReader xmlr)
  5. readEndTag(XMLStreamReader reader, String localName)
  6. readTextWithNodes(XMLStreamReader xsr, Map nodeIdToOffsetMap)
  7. skip(XMLStreamReader parser)
  8. skip(XMLStreamReader parser)
  9. skipEventsTo(int targetEvent, XMLStreamReader parser)