Example usage for javax.xml.stream XMLStreamReader getElementText

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

Introduction

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

Prototype

public String getElementText() throws XMLStreamException;

Source Link

Document

Reads the content of a text-only element, an exception is thrown if this is not a text-only element.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory xof = XMLOutputFactory.newFactory();

    StringWriter sw = new StringWriter();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(sw);
    xsw.writeStartDocument();//from w w  w  . jav a  2 s . c  o m
    xsw.writeStartElement("foo");
    xsw.writeCharacters("<>\"&'");
    xsw.writeEndDocument();

    String xml = sw.toString();
    System.out.println(xml);

    // READ THE XML
    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));
    xsr.nextTag(); // Advance to "foo" element
    System.out.println(xsr.getElementText());
}

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 .  j  a v a  2  s .  com
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(new File("test.xml")));

    int eventTypeID = reader.nextTag();
    reader.require(XMLStreamConstants.START_ELEMENT, null, "person");

    eventTypeID = reader.nextTag();//from ww  w . j  a v  a2s .com
    reader.require(XMLStreamConstants.START_ELEMENT, null, "first_name");
    System.out.println(reader.getElementText());

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(new File("test.xml")));

    int eventTypeID = reader.nextTag();
    reader.require(XMLStreamConstants.START_ELEMENT, null, "person");

    eventTypeID = reader.nextTag();/*w  w w .jav a2 s. c om*/
    try {
        reader.require(XMLStreamConstants.START_ELEMENT, null, "first_name");
    } catch (XMLStreamException e) {
        System.out.println("Assertion failed. " + e.getMessage() + " at " + reader.getLocation().getLineNumber()
                + ":" + reader.getLocation().getColumnNumber());
    }
    System.out.println(reader.getElementText());

}

From source file:Main.java

public static String readText(XMLStreamReader reader) throws XMLStreamException {
    return reader.getElementText();
    //      reader.nextTag();
    //      reader.require(XMLStreamConstants.CHARACTERS,null,null);
    //      return reader.getText();
}

From source file:net.cloudkit.enterprises.ws.SuperPassQueryTest.java

public static String parsingReceiptData(String responseData) throws XMLStreamException {
    StringBuilder dataBuilder = new StringBuilder("");
    XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(responseData));
    try {/* w w w.j  a  va  2s. co m*/
        int event = reader.getEventType();
        while (true) {
            switch (event) {
            case XMLStreamConstants.START_ELEMENT:

                if (reader.getName().toString().equals("TradeName")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                // System.out.println(reader.getName());
                if (reader.getName().toString().equals("SeqNo")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                // BillNo
                if (reader.getName().toString().equals("EntryId")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                if (reader.getName().toString().equals("IEFlag")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                // ??
                if (reader.getName().toString().equals("IEDate")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                if (reader.getName().toString().equals("TradeMode")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                if (reader.getName().toString().equals("Status")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                if (reader.getName().toString().equals("RetExplain")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                // NoticeDate
                // CustomsCode
                break;
            case XMLStreamConstants.END_ELEMENT:
                // System.out.println("End Element:" + r.getName());
                break;
            }
            if (!reader.hasNext())
                break;
            event = reader.next();
        }
    } finally {
        reader.close();
    }
    dataBuilder.append("\n");
    return dataBuilder.toString();
}

From source file:net.cloudkit.enterprises.ws.SuperPassQueryTest.java

public static String parsingReceiptStatus(String responseContext) throws XMLStreamException {
    String serviceResponseCode = "-1";
    XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(responseContext));
    try {/*from www  .jav  a 2s  .  co m*/
        int event = reader.getEventType();
        while (true) {
            switch (event) {
            case XMLStreamConstants.START_ELEMENT:
                // System.out.println(reader.getName());
                if (reader.getName().toString().equals("ServiceResponseCode")) {
                    // System.out.println(reader.getElementText());
                    serviceResponseCode = reader.getElementText();
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                // System.out.println("End Element:" + r.getName());
                break;
            }
            if (!reader.hasNext())
                break;
            event = reader.next();
        }
    } finally {
        reader.close();
    }
    return serviceResponseCode;
}

From source file:edu.stanford.cfuller.colocalization3d.correction.Correction.java

/**
 * Reads a stored correction from disk.//  w w w  .  j a va  2 s.c  o  m
 * 
 * @param filename                  The name of the file containing the Correction that was previously written to disk.
 * @return                          The Correction contained in the file.
 * @throws java.io.IOException      if the Correction cannot be successfully read.
 * @throws ClassNotFoundException   if the file does not contain a Correction.
 */
public static Correction readFromDisk(String filename) throws java.io.IOException, ClassNotFoundException {

    File f = new File(filename);

    FileReader fr = new FileReader(f);

    XMLStreamReader xsr = null;
    String encBinData = null;

    try {
        xsr = XMLInputFactory.newFactory().createXMLStreamReader(fr);

        while (xsr.hasNext()) {

            int event = xsr.next();

            if (event != XMLStreamReader.START_ELEMENT)
                continue;

            if (xsr.hasName() && xsr.getLocalName() == BINARY_DATA_ELEMENT) {

                encBinData = xsr.getElementText();

                break;

            }

        }
    } catch (XMLStreamException e) {
        java.util.logging.Logger.getLogger(LOG_NAME)
                .severe("Exception encountered while reading XML correction: " + e.getMessage());
    }
    byte[] binData = (new HexBinaryAdapter()).unmarshal(encBinData);

    ObjectInputStream oi = new ObjectInputStream(new ByteArrayInputStream(binData));

    Object o = oi.readObject();

    return (Correction) o;

}

From source file:org.javelin.sws.ext.bind.SoapEncodingMarshaller.java

@Override
public Object unmarshal(Source source) throws IOException, XmlMappingException {
    try {/*w  w  w  .  java 2 s  .  co m*/
        XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(source);
        streamReader.next();
        return streamReader.getElementText();
    } catch (XMLStreamException e) {
        throw new UnmarshallingFailureException(e.getMessage(), e);
    }
}

From source file:de.tuebingen.uni.sfs.germanet.api.IliLoader.java

/**
 * Returns an English synonym for the currently processed <code>IliRecord</code>
 * @param parser the <code>parser</code> being used on the current file
 * @return <code>String</code> representation of an English synonym
 * @throws javax.xml.stream.XMLStreamException
 *///from   ww w .  j a v a 2s.  c om
private String processEnglishSynonyms(XMLStreamReader parser) throws XMLStreamException {
    String englishSynonym = parser.getElementText();
    return englishSynonym;
}