Example usage for javax.xml.stream XMLStreamWriter writeEmptyElement

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

Introduction

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

Prototype

public void writeEmptyElement(String localName) throws XMLStreamException;

Source Link

Document

Writes an empty element tag to the output

Usage

From source file:EmptyElement.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out);
    writer.writeEmptyElement("empty");
    writer.writeAttribute("attribute", "true");
    writer.writeEndDocument();//  ww  w  .j  a  v  a2s  .com
    writer.flush(); // write /> to the console
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out);
    writer.setDefaultNamespace("http://www.example.com/ns1");
    writer.writeStartElement("http://www.example.com/ns1", "sample");
    writer.writeDefaultNamespace("http://www.example.com/ns1");
    writer.writeEmptyElement("inner");
    writer.setDefaultNamespace("http://www.example.com/ns2");
    writer.writeStartElement("http://www.example.com/ns2", "inner2");
    writer.writeDefaultNamespace("http://www.example.com/ns2");
    writer.writeNamespace("ns1", "http://www.example.com/ns1");
    writer.writeEmptyElement("http://www.example.com/ns1", "inner");

    writer.writeEndDocument();/*from  w  ww .  j  a v  a  2s  . 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();//www.  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: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 www  .j a  v a2 s .  com*/
        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: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//  w w  w  . j  a  v a2s . c  o m
        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:Main.java

public static void writeTag(XMLStreamWriter xmlWriter, String tag, Object value) throws XMLStreamException {
    if (value == null) {
        xmlWriter.writeEmptyElement(tag);
    } else {// w  w w  . j  a  v a2s.  com
        xmlWriter.writeStartElement(tag);
        xmlWriter.writeCData(value.toString());
        xmlWriter.writeEndElement();
    }
}

From source file:org.maodian.flyingcat.xmpp.codec.PresenceCodec.java

@Override
public void encode(Object object, XMLStreamWriter xmlsw) throws XMLStreamException {
    Presence p = (Presence) object;/*from   w w  w .ja v  a2  s  .  c  o m*/
    xmlsw.writeEmptyElement("presence");
    writeAttributeIfNotBlank(xmlsw, "id", p.getId());
    writeRequiredAttribute(xmlsw, "to", p.getTo().toBareJID());
    writeRequiredAttribute(xmlsw, "from",
            StringUtils.isBlank(p.getFrom().getResource()) ? p.getFrom().toBareJID() : p.getFrom().toFullJID());

    // if there is a 'type' attribute, then it's a request, otherwise is a notification
    if (p.getType() != null) {
        writeRequiredAttribute(xmlsw, "type", p.getType().name().toLowerCase());
    }
    xmlsw.writeEndDocument();
}

From source file:com.github.fritaly.graphml4j.GroupStyle.java

private void writeInsets(XMLStreamWriter writer) throws XMLStreamException {
    Validate.notNull(writer, "The given stream writer is null");

    writer.writeEmptyElement("y:BorderInsets");
    writer.writeAttribute("bottom", String.format("%.0f", insets));
    writer.writeAttribute("bottomF", String.format("%.1f", insets));
    writer.writeAttribute("left", String.format("%.0f", insets));
    writer.writeAttribute("leftF", String.format("%.1f", insets));
    writer.writeAttribute("right", String.format("%.0f", insets));
    writer.writeAttribute("rightF", String.format("%.1f", insets));
    writer.writeAttribute("top", String.format("%.0f", insets));
    writer.writeAttribute("topF", String.format("%.1f", insets));
}

From source file:com.github.fritaly.graphml4j.ShapeStyle.java

private void writeShape(XMLStreamWriter writer) throws XMLStreamException {
    Validate.notNull(writer, "The given stream writer is null");

    // y:Shape/*  w w  w . j  a  v  a2s. c o  m*/
    writer.writeEmptyElement("y:Shape");
    writer.writeAttribute("type", shape.getValue());
}

From source file:com.github.fritaly.graphml4j.GroupStyle.java

private void writeState(XMLStreamWriter writer, boolean closed) throws XMLStreamException {
    Validate.notNull(writer, "The given stream writer is null");

    writer.writeEmptyElement("y:State");
    writer.writeAttribute("closed", Boolean.toString(closed));
    writer.writeAttribute("closedHeight", String.format("%.1f", 50.0f)); // TODO Create property for closedHeight
    writer.writeAttribute("closedWidth", String.format("%.1f", 50.0f)); // TODO Create property for closedWidth

    // Infer the property innerGraphDisplayEnabled from the closed flag
    writer.writeAttribute("innerGraphDisplayEnabled", Boolean.toString(!closed));
}