Java XML Element Get Value getXMLContent(XMLEventReader reader, StartElement element, boolean decodeCharacters)

Here you can find the source of getXMLContent(XMLEventReader reader, StartElement element, boolean decodeCharacters)

Description

get XML Content

License

Open Source License

Declaration

public static String getXMLContent(XMLEventReader reader, StartElement element, boolean decodeCharacters)
            throws XMLStreamException 

Method Source Code

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

import javax.xml.namespace.QName;
import javax.xml.stream.*;
import javax.xml.stream.events.*;
import java.io.*;

import java.util.Iterator;

public class Main {
    public static String getXMLContent(XMLEventReader reader, StartElement element, boolean decodeCharacters)
            throws XMLStreamException {
        String rootElementName = getLocalName(element);

        StringWriter buffer = new StringWriter(1024);

        StartElement pendingElement = null;
        String pendingElementName = null;

        while (reader.hasNext()) {
            XMLEvent event = reader.nextEvent();

            if (pendingElement != null) {
                boolean skip = false;

                if (event.isEndElement() && pendingElementName.equals(getLocalName(event.asEndElement()))) {
                    writeAsEncodedUnicode(pendingElement, buffer, true); // empty tag
                    skip = true; // skip this end tag
                } else {
                    writeAsEncodedUnicode(pendingElement, buffer, false);
                }/*from   w  w w .  j  a  v  a2 s . c  om*/

                pendingElement = null;
                pendingElementName = null;

                if (skip)
                    continue;
            }

            if (event.isEndElement()) {
                EndElement endElement = event.asEndElement();
                String name = getLocalName(endElement);

                if (rootElementName.equals(name))
                    return buffer.toString();

                writeAsEncodedUnicode(endElement, buffer);
            } else if (event.isStartElement()) {
                pendingElement = event.asStartElement();
                pendingElementName = getLocalName(pendingElement);
            } else if (event.isCharacters() && decodeCharacters) {
                buffer.append(event.asCharacters().getData());
            } else {
                event.writeAsEncodedUnicode(buffer);
            }
        }

        throw new XMLStreamException(format("Missing closing tag for '" + rootElementName + "' element", element));
    }

    public static String getLocalName(StartElement element) {
        return element.getName().getLocalPart();
    }

    public static String getLocalName(EndElement element) {
        return element.getName().getLocalPart();
    }

    private static void writeAsEncodedUnicode(StartElement element, Writer writer, boolean isEmpty)
            throws XMLStreamException {
        try {
            // Write start tag.
            writer.write('<');
            QName name = element.getName();

            String prefix = name.getPrefix();
            if (prefix != null && prefix.length() > 0) {
                writer.write(prefix);
                writer.write(':');
            }
            writer.write(name.getLocalPart());

            // Write namespace declarations.
            Iterator nsIter = element.getNamespaces();
            while (nsIter.hasNext()) {
                Namespace ns = (Namespace) nsIter.next();
                writer.write(' ');
                ns.writeAsEncodedUnicode(writer);
            }

            // Write attributes
            Iterator attrIter = element.getAttributes();
            while (attrIter.hasNext()) {
                Attribute attr = (Attribute) attrIter.next();
                writer.write(' ');
                attr.writeAsEncodedUnicode(writer);
            }

            if (isEmpty)
                writer.write('/');
            writer.write('>');
        } catch (IOException ioe) {
            throw new XMLStreamException(ioe);
        }
    }

    private static void writeAsEncodedUnicode(EndElement element, Writer writer) throws XMLStreamException {
        try {
            // Write end tags.
            writer.write("</");
            QName name = element.getName();
            String prefix = name.getPrefix();
            if (prefix != null && prefix.length() > 0) {
                writer.write(prefix);
                writer.write(':');
            }
            writer.write(name.getLocalPart());
            writer.write('>');
        } catch (IOException ioe) {
            throw new XMLStreamException(ioe);
        }
    }

    private static String format(String message, XMLEvent event) {
        Location location = event == null ? null : event.getLocation();
        return location == null ? message : (message + " at line " + location.getLineNumber());
    }
}

Related

  1. getValue(Element pElement)
  2. getValueFromElement(Element element, String tagName)
  3. getValueFromElement(Element element, String tagName)
  4. getWholeText(Element element)
  5. getXmlBoolean(Element element, String name)
  6. getXmlElementAnnotation(Field field)
  7. getXmlElementDecl(Method method)
  8. getXMLElementTextValue(Element element, String tagName)
  9. getXMLIdentifier(Element element)