Example usage for javax.xml.stream XMLStreamReader getLocalName

List of usage examples for javax.xml.stream XMLStreamReader getLocalName

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader getLocalName.

Prototype

public String getLocalName();

Source Link

Document

Returns the (local) name of the current event.

Usage

From source file:StAXTest.java

public static void main(String[] args) throws Exception {
    String urlString;/*from ww  w .  j av  a 2s . c  om*/
    if (args.length == 0) {
        urlString = "http://www.w3c.org";
        System.out.println("Using " + urlString);
    } else
        urlString = args[0];
    URL url = new URL(urlString);
    InputStream in = url.openStream();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader parser = factory.createXMLStreamReader(in);
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (parser.getLocalName().equals("a")) {
                String href = parser.getAttributeValue(null, "href");
                if (href != null)
                    System.out.println(href);
            }
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("data.xml"));
    xsr.nextTag(); // advance to Employees tag
    xsr.nextTag(); // advance to first Employer element
    Map<String, String> map = new HashMap<String, String>();
    while (xsr.getLocalName().equals("Employee")) {
        map.put(xsr.getAttributeValue("", "id"), xsr.getElementText());
        xsr.nextTag(); // advance to next Employer element
    }/*from w ww  .ja  v  a  2  s .com*/
}

From source file:CursorApproachEventObject.java

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

    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    xmlif.setEventAllocator(new XMLEventAllocatorImpl());
    allocator = xmlif.getEventAllocator();

    XMLStreamReader xmlr = xmlif.createXMLStreamReader(filename, new FileInputStream(filename));

    int eventType = xmlr.getEventType();

    while (xmlr.hasNext()) {
        eventType = xmlr.next();//from w  w  w  .j av  a 2  s.c o m
        if ((eventType == XMLStreamConstants.START_ELEMENT) && xmlr.getLocalName().equals("Book")) {
            StartElement event = getXMLEvent(xmlr).asStartElement();
            System.out.println("EVENT: " + event.toString());
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader sr = factory.createXMLStreamReader(new FileReader("test.xml"));
    System.out.println(sr.getClass());

    while (sr.hasNext()) {
        int eventType = sr.next();

        if (eventType == XMLStreamReader.START_DOCUMENT) {
            continue;
        } else if (eventType == XMLStreamReader.END_ELEMENT) {
            System.out.println("End Element:    " + sr.getLocalName());
        } else if (eventType == XMLStreamReader.START_ELEMENT) {
            System.out.println("Start Element:  " + sr.getLocalName());
        }/*  ww w.  j  a va  2  s  .c o  m*/
    }
}

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());
        }/*from   w  w  w  . ja va2s  .  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

private static void positionXMLStreamReaderAtAnyElement(XMLStreamReader xsr) throws Exception {
    while (xsr.hasNext()) {
        if (xsr.getEventType() == XMLStreamReader.START_ELEMENT && "ANY-ELEMENT".equals(xsr.getLocalName())) {
            break;
        }/*w ww.  ja  va2 s .  c  o m*/
        xsr.next();
    }
}

From source file:Main.java

/**
 * Checks whether XML stream reader namespace and local name match the one
 * specified in the call./*from   www  .  j a v  a2  s. c om*/
 *
 * @param namespace namespace
 * @param localName local name
 * @param reader    XML stream reader
 *
 * @return true if namespace and local name match, otherwise false
 */
public static boolean isSameElement(final String namespace, final String localName,
        final XMLStreamReader reader) {
    return namespace.equals(reader.getNamespaceURI()) && localName.equals(reader.getLocalName());
}

From source file:Main.java

public static void skipToStart(XMLStreamReader xmlRdr, String elementName) throws XMLStreamException {
    while (xmlRdr.hasNext()) {
        if (xmlRdr.isStartElement() && xmlRdr.getLocalName().equals(elementName)) {
            break;
        }//from  w  w w  . jav  a2s . c  o m
        xmlRdr.next();
    }
}

From source file:Main.java

public static boolean isStartElement(XMLStreamReader xmlRdr, String tagName) {
    //return isElement(xmlRdr, XMLStreamConstants.START_ELEMENT, tagName);

    return xmlRdr.isStartElement() && xmlRdr.getLocalName().equals(tagName);
}

From source file:Main.java

/**
 * Test if reader is on an end tag named tagLocalName.
 *
 * @param reader       xml stream reader
 * @param tagLocalName tag local name//w  w  w  . jav  a 2  s  .c  o m
 * @return true if reader is on an end tag named tagLocalName
 */
public static boolean isEndTag(XMLStreamReader reader, String tagLocalName) {
    return (reader.getEventType() == XMLStreamConstants.END_ELEMENT)
            && (reader.getLocalName().equals(tagLocalName));
}