Example usage for javax.xml.stream XMLStreamWriter writeStartElement

List of usage examples for javax.xml.stream XMLStreamWriter writeStartElement

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamWriter writeStartElement.

Prototype

public void writeStartElement(String localName) throws XMLStreamException;

Source Link

Document

Writes a start tag to the output.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory xof = XMLOutputFactory.newFactory();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);

    xsw.writeStartDocument();//from w  w w. j  av  a 2s  .com
    xsw.writeStartElement("response");
    xsw.writeStartElement("message");
    xsw.writeCharacters("1 < 2");
    xsw.writeEndDocument();

    xsw.close();
}

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   www  . ja v a2  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 {
    OutputStream stream = System.out;

    XMLOutputFactory xof = XMLOutputFactory.newFactory();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(stream);

    xsw.writeStartDocument();/*from  ww  w .j  a  v a2  s  . com*/
    xsw.writeStartElement("foo");
    xsw.writeStartElement("bar");

    xsw.writeCharacters("");
    xsw.flush();

    OutputStreamWriter osw = new OutputStreamWriter(stream);
    osw.write("<baz>Hello World<baz>");
    osw.flush();

    xsw.writeEndElement();
    xsw.writeEndElement();
    xsw.writeEndDocument();
    xsw.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out);
    writer.writeStartDocument("1.0");
    writer.writeStartElement("person");
    writer.writeStartElement("name");
    writer.writeStartElement("fname");
    writer.writeCharacters("AAA");

    writer.writeEndDocument();/*from  www . j  a va  2 s  .  c o m*/
    writer.flush();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out);
    writer.writeStartDocument("1.0");
    writer.writeStartElement("addresses");
    writer.writeStartElement("address");
    writer.writeAttribute("type", "work");
    writer.writeStartElement("street");
    writer.writeCharacters("1111 Ave");
    writer.writeComment("comments");
    writer.writeEndElement();/*ww  w  .  ja va 2 s . c  om*/
    writer.writeEmptyElement("inner");
    writer.flush();
    System.out.println();
    writer.writeAttribute("otherAttribute", "true");
    writer.flush();
    System.out.println();
    writer.writeEndElement();
    writer.writeEndElement();
    writer.writeEndDocument();
    writer.flush();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = factory.createXMLStreamWriter(System.out);

    writer.writeStartDocument("1.0");

    writer.writeStartElement("catalog");

    writer.writeStartElement("book");

    writer.writeAttribute("id", "1");

    writer.writeStartElement("code");
    writer.writeCharacters("I01");
    writer.writeEndElement();//w w w  .j a  va  2 s .  c  om

    writer.writeStartElement("title");
    writer.writeCharacters("This is the title");
    writer.writeEndElement();

    writer.writeStartElement("price");
    writer.writeCharacters("$2.95");
    writer.writeEndElement();

    writer.writeEndDocument();

    writer.flush();
    writer.close();
}

From source file:fr.ritaly.dungeonmaster.item.ItemDef.java

public static void main(String[] args) throws Exception {
    final List<Item.Type> types = Arrays.asList(Item.Type.values());

    Collections.sort(types, new Comparator<Item.Type>() {
        @Override/* ww  w . ja va2s . c  om*/
        public int compare(Type o1, Type o2) {
            return o1.name().compareTo(o2.name());
        }
    });

    final StringWriter stringWriter = new StringWriter(32000);

    final XMLStreamWriter writer = new IndentingXMLStreamWriter(
            XMLOutputFactory.newFactory().createXMLStreamWriter(stringWriter));

    writer.writeStartDocument();
    writer.writeStartElement("items");
    writer.writeDefaultNamespace("yadmjc:items:1.0");

    for (Item.Type type : types) {
        writer.writeStartElement("item");
        writer.writeAttribute("id", type.name());
        writer.writeAttribute("weight", Float.toString(type.getWeight()));

        if (type.getDamage() != -1) {
            writer.writeAttribute("damage", Integer.toString(type.getDamage()));
        }

        if (type.getActivationBodyPart() != null) {
            writer.writeAttribute("activation", type.getActivationBodyPart().name());
        }

        if (type.getShield() != 0) {
            writer.writeAttribute("shield", Integer.toString(type.getShield()));
        }
        if (type.getAntiMagic() != 0) {
            writer.writeAttribute("anti-magic", Integer.toString(type.getAntiMagic()));
        }

        if (type.getDecayRate() != -1) {
            writer.writeAttribute("decay-rate", Integer.toString(type.getDecayRate()));
        }

        if (type.getDistance() != -1) {
            writer.writeAttribute("distance", Integer.toString(type.getDistance()));
        }

        if (type.getShootDamage() != -1) {
            writer.writeAttribute("shoot-damage", Integer.toString(type.getShootDamage()));
        }

        if (!type.getCarryLocations().isEmpty()) {
            writer.writeStartElement("locations");

            // Sort the locations to ensure they're always serialized in a consistent way
            for (CarryLocation location : new TreeSet<CarryLocation>(type.getCarryLocations())) {
                writer.writeEmptyElement("location");
                writer.writeAttribute("id", location.name());
            }
            writer.writeEndElement();
        }

        if (!type.getActions().isEmpty()) {
            writer.writeStartElement("actions");
            for (ActionDef actionDef : type.getActions()) {
                writer.writeEmptyElement("action");
                writer.writeAttribute("id", actionDef.getAction().name());

                if (actionDef.getMinLevel() != Champion.Level.NONE) {
                    writer.writeAttribute("min-level", actionDef.getMinLevel().name());
                }

                if (actionDef.isUseCharges()) {
                    writer.writeAttribute("use-charges", Boolean.toString(actionDef.isUseCharges()));
                }
            }
            writer.writeEndElement(); // </actions>
        }

        if (!type.getEffects().isEmpty()) {
            writer.writeStartElement("effects");
            for (Effect effect : type.getEffects()) {
                writer.writeEmptyElement("effect");
                writer.writeAttribute("stat", effect.getStatistic().name());
                writer.writeAttribute("strength", String.format("%+d", effect.getStrength()));
            }
            writer.writeEndElement(); // </effects>
        }

        writer.writeEndElement(); // </item>
    }

    writer.writeEndElement(); // </items>
    writer.writeEndDocument();

    System.out.println(stringWriter);
}

From source file:fr.ritaly.dungeonmaster.ai.CreatureDef.java

public static void main(String[] args) throws Exception {
    final List<Creature.Type> types = Arrays.asList(Creature.Type.values());

    Collections.sort(types, new Comparator<Creature.Type>() {
        @Override//from  w  w  w .ja  v a  2  s . c  om
        public int compare(Creature.Type o1, Creature.Type o2) {
            return o1.name().compareTo(o2.name());
        }
    });

    final StringWriter stringWriter = new StringWriter(32000);

    final XMLStreamWriter writer = new IndentingXMLStreamWriter(
            XMLOutputFactory.newFactory().createXMLStreamWriter(stringWriter));

    writer.writeStartDocument();
    writer.writeStartElement("creatures");
    writer.writeDefaultNamespace("yadmjc:creatures:1.0");

    for (Creature.Type type : types) {
        writer.writeStartElement("creature");
        writer.writeAttribute("id", type.name());
        writer.writeAttribute("base-health", Integer.toString(type.getBaseHealth()));
        writer.writeAttribute("height", type.getHeight().name());
        writer.writeAttribute("size", type.getSize().name());
        writer.writeAttribute("awareness", Integer.toString(type.getAwareness()));
        writer.writeAttribute("bravery", Integer.toString(type.getBravery()));
        writer.writeAttribute("experience-multiplier", Integer.toString(type.getExperienceMultiplier()));
        writer.writeAttribute("move-duration", Integer.toString(type.getMoveDuration()));
        writer.writeAttribute("sight-range", Integer.toString(type.getSightRange()));
        writer.writeAttribute("absorbs-items", Boolean.toString(type.isAbsorbsItems()));
        writer.writeAttribute("levitates", Boolean.toString(type.levitates()));
        writer.writeAttribute("archenemy", Boolean.toString(type.isArchenemy()));
        writer.writeAttribute("night-vision", Boolean.toString(type.isNightVision()));
        writer.writeAttribute("sees-invisible", Boolean.toString(type.isSeesInvisible()));

        writer.writeEmptyElement("defense");
        writer.writeAttribute("anti-magic", Integer.toString(type.getAntiMagic()));
        writer.writeAttribute("armor", Integer.toString(type.getArmor()));
        writer.writeAttribute("shield", Integer.toString(type.getShield()));
        writer.writeAttribute("poison", Integer.toString(type.getPoisonResistance()));

        writer.writeEmptyElement("attack");
        writer.writeAttribute("skill", type.getAttackSkill().name());
        writer.writeAttribute("animation-duration", Integer.toString(type.getAttackAnimationDuration()));
        writer.writeAttribute("duration", Integer.toString(type.getAttackDuration()));
        writer.writeAttribute("power", Integer.toString(type.getAttackPower()));
        writer.writeAttribute("type", type.getAttackType().name());
        writer.writeAttribute("range", Integer.toString(type.getAttackRange()));
        writer.writeAttribute("probability", Integer.toString(type.getAttackProbability()));
        writer.writeAttribute("side-attack", Boolean.toString(type.isSideAttackAllowed()));

        writer.writeEmptyElement("poison");
        if (type.getPoison() != 0) {
            writer.writeAttribute("strength", Integer.toString(type.getPoison()));
        }

        if (!type.getSpells().isEmpty()) {
            writer.writeStartElement("spells");

            // Sort the spells to ensure they're always serialized in a
            // consistent way
            for (Spell.Type spell : new TreeSet<Spell.Type>(type.getSpells())) {
                writer.writeEmptyElement("spell");
                writer.writeAttribute("id", spell.name());
            }

            writer.writeEndElement(); // </spells>
        }

        if (!type.getWeaknesses().isEmpty()) {
            writer.writeStartElement("weaknesses");

            // Sort the weaknesses to ensure they're always serialized in a
            // consistent way
            for (Weakness weakness : new TreeSet<Weakness>(type.getWeaknesses())) {
                writer.writeEmptyElement("weakness");
                writer.writeAttribute("id", weakness.name());
            }

            writer.writeEndElement(); // </weaknesses>
        }

        if (!type.getDefinition().getItemDefs().isEmpty()) {
            writer.writeStartElement("items");

            for (ItemDef itemDef : type.getDefinition().getItemDefs()) {
                writer.writeEmptyElement("item");
                writer.writeAttribute("type", itemDef.type.name());
                writer.writeAttribute("min", Integer.toString(itemDef.min));
                writer.writeAttribute("max", Integer.toString(itemDef.max));

                if (itemDef.curse != null) {
                    writer.writeAttribute("curse", itemDef.curse.name());
                }
            }

            writer.writeEndElement(); // </items>
        }

        writer.writeEndElement(); // </creature>
    }

    writer.writeEndElement(); // </creatures>
    writer.writeEndDocument();

    System.out.println(stringWriter);
}

From source file:Main.java

public static void writeValue(XMLStreamWriter writer, String name, String value) throws XMLStreamException {
    writer.writeStartElement(name);
    writer.writeCharacters(value);/*from  w  w  w . j a  va 2  s.c  o m*/
    writer.writeEndElement();
}

From source file:Main.java

/**
 * write xml element start tag, data and end tag into XmlStreamWriter
 *
 * @param writer// w w  w . ja  va  2s . c  o  m
 * @param element
 * @param value
 * @throws XMLStreamException
 */
public static void writeSimpleDataElement(XMLStreamWriter writer, String element, String value)
        throws XMLStreamException {
    writer.writeStartElement(element);
    writer.writeCharacters(value);
    writer.writeEndElement();
}