Example usage for javax.xml.stream XMLStreamWriter writeAttribute

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

Introduction

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

Prototype

public void writeAttribute(String localName, String value) throws XMLStreamException;

Source Link

Document

Writes an attribute to the output stream without a prefix.

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();//from www. jav a 2 s . c  o  m
    writer.flush(); // write /> to the console
}

From source file:MainClass.java

public static void main(String[] args) throws XMLStreamException {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out);
    writer.writeStartDocument("1.0");

    writer.writeStartElement("http://www.t.com/f", "sample");

    writer.writeAttribute("attribute", "true");
    writer.writeAttribute("http://www.t.com/f", "attribute2", "false");
    writer.writeCharacters("some text");
    writer.writeCData("<test>");
    writer.writeEndElement();/*  w ww. java 2s  .  co  m*/
    writer.writeEndDocument();
    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. j  a v  a2  s .com*/
    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();//from   w ww.  java  2  s  .  c o  m

    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: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.writeCharacters("\n");
    writer.writeStartElement("ns1", "sample", "http://www.e.com/ns1");
    writer.writeNamespace("ns1", "http://www.e.com/ns1");

    writer.writeEmptyElement("http://www.e.com/ns1", "inner1");
    writer.writeAttribute("otherAttribute", "true");
    writer.writeEndElement();/*from w w w. j a  va2s  . c o  m*/
    writer.writeEndDocument();
    writer.flush();
    System.out.println();

}

From source file:CursorWriter.java

public static void main(String[] args) throws Exception {
    String fileName = "yourXML.xml";
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    XMLStreamWriter xtw = null;
    xtw = xof.createXMLStreamWriter(new FileWriter(fileName));
    xtw.writeComment("all elements here are explicitly in the HTML namespace");
    xtw.writeStartDocument("utf-8", "1.0");
    xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40");
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html");
    xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40");
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head");
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title");
    xtw.writeCharacters("character");
    xtw.writeEndElement();//from  w w  w .java2  s  . c  o m
    xtw.writeEndElement();
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body");
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p");
    xtw.writeCharacters("another character");
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a");
    xtw.writeAttribute("href", "http://www.java2s.com");
    xtw.writeCharacters("here");
    xtw.writeEndElement();
    xtw.writeEndElement();
    xtw.writeEndElement();
    xtw.writeEndElement();
    xtw.writeEndDocument();
    xtw.flush();
    xtw.close();
    System.out.println("Done");
}

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  a 2s .com*/
        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// ww  w. j a v  a2  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 writeInt(String name, int value, XMLStreamWriter writer) throws XMLStreamException {
    writer.writeAttribute(name, Integer.toString(value));
}

From source file:Main.java

public static void writeFloat(String name, float value, XMLStreamWriter writer) throws XMLStreamException {
    if (!Float.isNaN(value)) {
        writer.writeAttribute(name, Float.toString(value));
    }//w  ww.  j  av a  2 s  .  c  o  m
}