Example usage for javax.xml.stream XMLStreamWriter writeDefaultNamespace

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

Introduction

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

Prototype

public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException;

Source Link

Document

Writes the default namespace to the stream

Usage

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   www . j  av  a 2s.  c  o  m
    writer.flush();
}

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//from  www . j a  v  a  2 s.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: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  av 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

/**
 * Borrowed from org.apache.xml.security.test.stax.utils.XmlReaderToWriter
 *//*from   w  ww .j  a  v  a  2  s. c o  m*/
public static void write(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException {
    switch (xmlr.getEventType()) {
    case XMLEvent.START_ELEMENT:
        final String localName = xmlr.getLocalName();
        final String namespaceURI = xmlr.getNamespaceURI();
        if (namespaceURI != null && namespaceURI.length() > 0) {
            final String prefix = xmlr.getPrefix();
            if (prefix != null) {
                writer.writeStartElement(prefix, localName, namespaceURI);
            } else {
                writer.writeStartElement(namespaceURI, localName);
            }
        } else {
            writer.writeStartElement(localName);
        }

        for (int i = 0, len = xmlr.getNamespaceCount(); i < len; i++) {
            String prefix = xmlr.getNamespacePrefix(i);
            if (prefix == null) {
                writer.writeDefaultNamespace(xmlr.getNamespaceURI(i));
            } else {
                writer.writeNamespace(prefix, xmlr.getNamespaceURI(i));
            }
        }

        for (int i = 0, len = xmlr.getAttributeCount(); i < len; i++) {
            final String attUri = xmlr.getAttributeNamespace(i);

            if (attUri != null && attUri.length() > 0) {
                final String prefix = xmlr.getAttributePrefix(i);
                if (prefix != null) {
                    writer.writeAttribute(prefix, attUri, xmlr.getAttributeLocalName(i),
                            xmlr.getAttributeValue(i));
                } else {
                    writer.writeAttribute(attUri, xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
                }
            } else {
                writer.writeAttribute(xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
            }

        }
        break;
    case XMLEvent.END_ELEMENT:
        writer.writeEndElement();
        break;
    case XMLEvent.SPACE:
    case XMLEvent.CHARACTERS:
        char[] text = new char[xmlr.getTextLength()];
        xmlr.getTextCharacters(0, text, 0, xmlr.getTextLength());
        writer.writeCharacters(text, 0, text.length);
        break;
    case XMLEvent.PROCESSING_INSTRUCTION:
        writer.writeProcessingInstruction(xmlr.getPITarget(), xmlr.getPIData());
        break;
    case XMLEvent.CDATA:
        writer.writeCData(xmlr.getText());
        break;
    case XMLEvent.COMMENT:
        writer.writeComment(xmlr.getText());
        break;
    case XMLEvent.ENTITY_REFERENCE:
        writer.writeEntityRef(xmlr.getLocalName());
        break;
    case XMLEvent.START_DOCUMENT:
        String encoding = xmlr.getCharacterEncodingScheme();
        String version = xmlr.getVersion();

        if (encoding != null && version != null) {
            writer.writeStartDocument(encoding, version);
        } else if (version != null) {
            writer.writeStartDocument(xmlr.getVersion());
        }
        break;
    case XMLEvent.END_DOCUMENT:
        writer.writeEndDocument();
        break;
    case XMLEvent.DTD:
        writer.writeDTD(xmlr.getText());
        break;
    }
}

From source file:org.maodian.flyingcat.xmpp.extensions.xep0030.QueryItemCodec.java

@Override
public void encode(Object object, XMLStreamWriter xmlsw) throws XMLStreamException {
    xmlsw.writeStartElement("", "query", ServiceDiscovery.ITEM);
    xmlsw.writeDefaultNamespace(ServiceDiscovery.ITEM);
}

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

@Override
public void encode(Object object, XMLStreamWriter xmlsw) throws XMLStreamException {
    Bind bind = (Bind) object;/*  w  w  w  .j  a va2 s .  c om*/
    xmlsw.writeStartElement("bind");
    xmlsw.writeDefaultNamespace(XmppNamespace.BIND);

    xmlsw.writeStartElement("jid");
    xmlsw.writeCharacters(bind.getJabberId());

    xmlsw.writeEndElement();
    xmlsw.writeEndElement();
}

From source file:org.maodian.flyingcat.xmpp.extensions.xep0030.QueryInfoCodec.java

@Override
public void encode(Object object, XMLStreamWriter xmlsw) throws XMLStreamException {
    xmlsw.writeStartElement("", "query", ServiceDiscovery.INFORMATION);
    xmlsw.writeDefaultNamespace(ServiceDiscovery.INFORMATION);

    QueryInfo qi = (QueryInfo) object;/* w  w w .ja v a 2 s.com*/
    List<Identity> identityList = qi.getIdentityList();
    for (Identity identity : identityList) {
        xmlsw.writeEmptyElement("", "identity", ServiceDiscovery.INFORMATION);
        xmlsw.writeAttribute("category", identity.getCategory());
        xmlsw.writeAttribute("type", identity.getType());
    }

    List<Feature> featureList = qi.getFeatureList();
    for (Feature feature : featureList) {
        xmlsw.writeEmptyElement(ServiceDiscovery.INFORMATION, "feature");
        xmlsw.writeAttribute("var", feature.getVar());
    }
}

From source file:org.maodian.flyingcat.xmpp.extensions.xep0077.RegistrationCodec.java

@Override
public void encode(Object object, XMLStreamWriter xmlsw) throws XMLStreamException {
    xmlsw.writeStartElement("", "query", InBandRegistration.REGISTER);
    xmlsw.writeDefaultNamespace(InBandRegistration.REGISTER);

    xmlsw.writeEmptyElement(InBandRegistration.REGISTER, "username");
    xmlsw.writeEmptyElement(InBandRegistration.REGISTER, "password");

    xmlsw.writeEndElement();/*from  ww  w  .ja  v a  2  s  .c  o m*/
}

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

@Override
public void encode(Object object, XMLStreamWriter xmlsw) throws XMLStreamException {
    Roster roster = (Roster) object;//ww w.ja va2 s .c  o m
    xmlsw.writeStartElement("", "query", XmppNamespace.ROSTER);
    xmlsw.writeDefaultNamespace(XmppNamespace.ROSTER);
    writeAttributeIfNotBlank(xmlsw, "ver", roster.getVersion());
    for (Contact c : roster) {
        xmlsw.writeEmptyElement("", "item", XmppNamespace.ROSTER);
        writeRequiredAttribute(xmlsw, "jid", c.getJabberId());
        writeAttributeIfNotBlank(xmlsw, "name", c.getName());
        writeAttributeIfNotBlank(xmlsw, "subscription", c.getSubscription());
        writeAttributeIfNotBlank(xmlsw, "ask", c.getAsk());
    }
    xmlsw.writeEndElement();
}

From source file:de.shadowhunt.subversion.internal.httpv1.CheckoutOperation.java

@Override
protected HttpUriRequest createRequest() {
    final Writer body = new StringBuilderWriter();
    try {//from   ww  w . j av  a 2  s .  c  om
        final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(body);
        writer.writeStartDocument(XmlConstants.ENCODING, XmlConstants.VERSION_1_0);
        writer.writeStartElement("checkout");
        writer.writeDefaultNamespace(XmlConstants.DAV_NAMESPACE);
        writer.writeStartElement("activity-set");
        writer.writeStartElement("href");
        final URI transactionURI = URIUtils.createURI(repository, transaction);
        writer.writeCData(transactionURI.toString());
        writer.writeEndElement(); // href
        writer.writeEndElement(); // activity-set
        writer.writeEmptyElement("apply-to-version");
        writer.writeEndElement(); //checkout
        writer.writeEndDocument();
        writer.close();
    } catch (final XMLStreamException e) {
        throw new SubversionException("could not create request body", e);
    }

    final URI uri = URIUtils.createURI(repository, resource);
    final DavTemplateRequest request = new DavTemplateRequest("CHECKOUT", uri);
    request.setEntity(new StringEntity(body.toString(), CONTENT_TYPE_XML));
    return request;
}