Example usage for org.jdom2 Element getAttribute

List of usage examples for org.jdom2 Element getAttribute

Introduction

In this page you can find the example usage for org.jdom2 Element getAttribute.

Prototype

public Attribute getAttribute(final String attname) 

Source Link

Document

This returns the attribute for this element with the given name and within no namespace, or null if no such attribute exists.

Usage

From source file:mx.com.pixup.portal.dao.DiscoParserDaoJdbc.java

public void parserXML() {

    try {//from w  w w  .j av  a 2s  .com

        //variables BD
        String sql = "INSERT INTO disco VALUES (?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement preparedStatement;
        Connection connection = dataSource.getConnection();
        connection.setAutoCommit(false);

        //se obtiene elemento raiz
        Element discos = this.xmlDocument.getRootElement();
        //elementos 2do nivel
        List<Element> listaDiscos = discos.getChildren();
        Iterator<Element> i = listaDiscos.iterator();

        while (i.hasNext()) {
            Element disco = i.next();

            Attribute id = disco.getAttribute("id");
            Attribute idioma = disco.getAttribute("idioma");
            Attribute pais = disco.getAttribute("pais");
            Attribute disquera = disco.getAttribute("disquera");
            Attribute genero = disco.getAttribute("genero");

            //Elementos de 3er nivel
            Element titulo = disco.getChild("titulo");
            Element fechalanzamiento = disco.getChild("fechalanzamiento");
            Element precio = disco.getChild("precio");
            Element cantidad = disco.getChild("cantidad");
            Element promocion = disco.getChild("promocion");
            Element iva = disco.getChild("iva");

            //construye parametros de la query
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id.getIntValue());
            preparedStatement.setString(2, titulo.getText());
            preparedStatement.setString(3, fechalanzamiento.getText());
            preparedStatement.setDouble(4, Double.parseDouble(precio.getText()));
            preparedStatement.setInt(5, Integer.parseInt(cantidad.getText()));
            preparedStatement.setInt(6, idioma.getIntValue());
            preparedStatement.setInt(7, pais.getIntValue());
            preparedStatement.setInt(8, disquera.getIntValue());
            preparedStatement.setInt(9, genero.getIntValue());
            preparedStatement.setInt(10, Integer.parseInt(promocion.getText()));
            preparedStatement.setInt(11, Integer.parseInt(iva.getText()));

            preparedStatement.execute();
        }

        connection.commit();
    } catch (Exception e) {
        //*** se quit el return porque el mtodo es void
        System.out.println(e.getMessage());
    }

}

From source file:mx.com.pixup.portal.dao.EstadoMunicipioParserDaoJdbc.java

public void parserXML() {

    try {/*from w ww .j ava2s .  c  o  m*/

        //variables BD
        String sql = "INSERT INTO estado VALUES (?,?)";
        String sqlM = "INSERT INTO municipio VALUES (?,?,?)";
        PreparedStatement preparedStatement;
        Connection connection = dataSource.getConnection();
        connection.setAutoCommit(false);

        //se obtiene elemento raiz
        Element estado = this.xmlDocumento.getRootElement();
        //elementos 2do nivel
        Element nombre = estado.getChild("nombre");
        Element municipios = estado.getChild("municipios");
        Attribute id = estado.getAttribute("id");
        //construye parametros de la query
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, id.getIntValue());
        preparedStatement.setString(2, nombre.getText());
        preparedStatement.execute();

        List<Element> listaMunicipios = municipios.getChildren("municipio");
        Iterator<Element> i = listaMunicipios.iterator();

        while (i.hasNext()) {
            Element municipio = i.next();
            //Elementos de tercer nivel
            Attribute idMunicipio = municipio.getAttribute("id");
            String nombreMunicipio = municipio.getText();
            //construye parametros de la query
            preparedStatement = connection.prepareStatement(sqlM);
            preparedStatement.setInt(1, idMunicipio.getIntValue());
            preparedStatement.setString(2, nombreMunicipio);
            preparedStatement.setInt(3, id.getIntValue());

            preparedStatement.execute();
        }

        connection.commit();
    } catch (Exception e) {
        //*** se quit el return porque el mtodo es void
        System.out.println(e.getMessage());
    }

}

From source file:neon.common.resources.loaders.ItemLoader.java

License:Open Source License

@Override
public RItem load(String id) throws IOException {
    Element root = files.loadFile(translator, namespace, id + ".xml").getRootElement();
    String name = root.getAttributeValue("name");
    char glyph = root.getChild("graphics").getAttributeValue("char").charAt(0);
    Color color = Color.web(root.getChild("graphics").getAttributeValue("color"));

    RItem.Builder builder = new RItem.Builder(id, name).setGraphics(glyph, color);

    if (root.getAttribute("price") != null) {
        builder.setPrice(Integer.parseInt(root.getAttributeValue("price")));
    }//from   w  w  w.j a v a2s.  c  o m

    if (root.getAttribute("weight") != null) {
        builder.setWeight(Integer.parseInt(root.getAttributeValue("weight")));
    }

    Element magic = root.getChild("magic");
    if (magic != null) {
        Effect effect = Effect.valueOf(magic.getAttributeValue("effect").toUpperCase());
        int magnitude = Integer.parseInt(magic.getAttributeValue("magnitude"));
        builder.setMagic(effect, magnitude);
    }

    switch (root.getName()) {
    case "armor":
        return createArmor(root, builder);
    case "clothing":
        return createClothing(root, builder);
    case "weapon":
        return createWeapon(root, builder);
    case "coin":
        return new RItem.Coin(builder);
    case "container":
        return new RItem.Container(builder);
    case "potion":
        return new RItem.Potion(builder);
    default:
        return builder.build();
    }
}

From source file:neon.core.GameLoader.java

License:Open Source License

private void loadEvents(Element events) {
    // gewone tasks
    for (Element event : events.getChildren("task")) {
        String description = event.getAttributeValue("desc");
        if (event.getAttribute("script") != null) {
            String script = event.getAttributeValue("script");
            queue.add(description, new ScriptAction(script));
        }//from  w  ww .j  a v a 2  s. co  m
    }

    // getimede tasks
    for (Element event : events.getChildren("timer")) {
        String[] ticks = event.getAttributeValue("tick").split(":");
        int start = Integer.parseInt(ticks[0]);
        int period = Integer.parseInt(ticks[1]);
        int stop = Integer.parseInt(ticks[2]);

        switch (event.getAttributeValue("task")) {
        case "script":
            queue.add(event.getAttributeValue("script"), start, period, stop);
            break;
        case "magic":
            Effect effect = Effect.valueOf(event.getAttributeValue("effect").toUpperCase());
            float magnitude = Float.parseFloat(event.getAttributeValue("magnitude"));
            String script = event.getAttributeValue("script");
            SpellType type = SpellType.valueOf(event.getAttributeValue("type").toUpperCase());
            Entity caster = null;
            if (event.getAttribute("caster") != null) {
                caster = Engine.getGame().getEntity(Long.parseLong(event.getAttributeValue("caster")));
            }
            Entity target = null;
            if (event.getAttribute("target") != null) {
                target = Engine.getGame().getEntity(Long.parseLong(event.getAttributeValue("target")));
            }
            Spell spell = new Spell(target, caster, effect, magnitude, script, type);
            queue.add(new MagicTask(spell, stop), start, stop, period);
            break;
        }
    }
}

From source file:neon.editor.resources.IContainer.java

License:Open Source License

public IContainer(Element properties) {
    super(properties);
    for (Element e : properties.getChildren("item")) {
        RItem ri = (RItem) Editor.resources.getResource(e.getAttributeValue("id"));
        contents.add(new IObject(ri, 0, 0, 0, Integer.parseInt(e.getAttributeValue("uid"))));
    }/*from  w w  w . j av  a 2s . com*/
    if (properties.getAttribute("lock") != null) {
        lock = Integer.parseInt(properties.getAttributeValue("lock"));
        key = (RItem) Editor.resources.getResource(properties.getAttributeValue("key"));
    }
    if (properties.getAttribute("trap") != null) {
        trap = Integer.parseInt(properties.getAttributeValue("trap"));
        spell = (RSpell.Enchantment) Editor.resources.getResource(properties.getAttributeValue("spell"),
                "magic");
    }
}

From source file:neon.editor.resources.IDoor.java

License:Open Source License

public IDoor(Element properties, RZone zone) {
    super(properties);
    if (properties.getAttribute("state") != null) {
        state = State.valueOf(properties.getAttributeValue("state"));
    } else {/* ww w  .j a va2s.co  m*/
        state = State.open;
    }
    if (properties.getAttribute("lock") != null) {
        lock = Integer.parseInt(properties.getAttributeValue("lock"));
        key = (RItem) Editor.resources.getResource(properties.getAttributeValue("key"));
    }
    if (properties.getAttribute("trap") != null) {
        trap = Integer.parseInt(properties.getAttributeValue("trap"));
        spell = (RSpell.Enchantment) Editor.resources.getResource(properties.getAttributeValue("spell"),
                "magic");
    }
    if (properties.getChild("dest") != null) {
        Element dest = properties.getChild("dest");
        if (dest.getAttribute("theme") != null) {
            destTheme = (RDungeonTheme) Editor.resources.getResource(dest.getAttributeValue("theme"), "theme");
        } else {
            if (dest.getAttribute("map") != null) {
                int uid = Integer.parseInt(dest.getAttributeValue("map"));
                for (RMap map : Editor.resources.getResources(RMap.class)) {
                    if (map.uid == uid) {
                        destMap = map;
                    }
                }
            } else {
                destMap = zone.map;
            }
            if (dest.getAttribute("z") != null) {
                destZone = destMap.getZone(Integer.parseInt(dest.getAttributeValue("z")));
            } else {
                destZone = destMap.getZone(0);
            }
            if (dest.getAttribute("x") != null && dest.getAttribute("y") != null) {
                int x = Integer.parseInt(dest.getAttributeValue("x"));
                int y = Integer.parseInt(dest.getAttributeValue("y"));
                destPos = new Point(x, y);
            }
        }
        text = dest.getAttributeValue("sign");
    }
}

From source file:neon.editor.resources.Instance.java

License:Open Source License

public Instance(Element properties) {
    x = Integer.parseInt(properties.getAttributeValue("x"));
    y = Integer.parseInt(properties.getAttributeValue("y"));
    if (properties.getAttribute("w") != null) {
        width = Integer.parseInt(properties.getAttributeValue("w"));
    }/*  www . j  a v a 2s  .  c o  m*/
    if (properties.getAttribute("h") != null) {
        height = Integer.parseInt(properties.getAttributeValue("h"));
    }
    if (properties.getAttribute("l") != null) {
        z = Byte.parseByte(properties.getAttributeValue("l"));
    }
}

From source file:neon.maps.MapLoader.java

License:Open Source License

private static Dungeon loadDungeon(Element root, int uid) {
    if (root.getChild("header").getAttribute("theme") != null) {
        String name = root.getChild("header").getChildText("name");
        return loadThemedDungeon(name, root.getChild("header").getAttributeValue("theme"), uid);
    }//from w w w.j  a  v a  2s .co m

    Dungeon map = new Dungeon(root.getChild("header").getChildText("name"), uid);

    for (Element l : root.getChildren("level")) {
        int level = Integer.parseInt(l.getAttributeValue("l"));
        String name = l.getAttributeValue("name");
        if (l.getAttribute("theme") != null) {
            RZoneTheme theme = (RZoneTheme) Engine.getResources().getResource(l.getAttributeValue("theme"),
                    "theme");
            map.addZone(level, name, theme);
            if (l.getAttribute("out") != null) {
                String[] connections = l.getAttributeValue("out").split(",");
                for (String connection : connections) {
                    map.addConnection(level, Integer.parseInt(connection));
                }
            }
        } else {
            map.addZone(level, name);
            loadZone(l, map, level, uid);
        }
    }

    return map;
}

From source file:neon.maps.MapLoader.java

License:Open Source License

private static Door loadDoor(Element door, String id, int x, int y, long itemUID, int mapUID) {
    Door d = (Door) EntityFactory.getItem(id, x, y, itemUID);

    // lock difficulty
    int lock = 0;
    if (door.getAttribute("lock") != null) {
        lock = Integer.parseInt(door.getAttributeValue("lock"));
        d.lock.setLockDC(lock);/* w  ww  .j ava2  s  .c  om*/
    }
    // sleutel
    if (door.getAttribute("key") != null) {
        RItem key = (RItem) Engine.getResources().getResource(door.getAttributeValue("key"));
        d.lock.setKey(key);
    }
    // state van de deur (open, dicht of gesloten)
    if (door.getAttributeValue("state").equals("locked")) {
        if (lock > 0) {
            d.lock.setState(Lock.LOCKED);
        } else { // als er geen lock is, state in closed veranderen
            d.lock.setState(Lock.CLOSED);
        }
    } else if (door.getAttributeValue("state").equals("closed")) {
        d.lock.setState(Lock.CLOSED);
    }

    // trap
    int trap = 0;
    if (door.getAttribute("trap") != null) {
        trap = Integer.parseInt(door.getAttributeValue("trap"));
        d.trap.setTrapDC(trap);
    }
    // spell
    if (door.getAttribute("spell") != null) {
        String spell = door.getAttributeValue("spell");
        RSpell.Enchantment enchantment = (RSpell.Enchantment) Engine.getResources().getResource(spell, "magic");
        d.setMagicComponent(new Enchantment(enchantment, 0, d.getUID()));
    }

    // bestemming van de deur
    Element dest = door.getChild("dest");
    Point destPos = null;
    int destLevel = 0;
    int destMapUID = 0;
    String theme = null;
    String sign = null;
    if (door.getChild("dest") != null) {
        int destX = -1;
        int destY = -1;
        if (dest.getAttribute("x") != null) {
            destX = Integer.parseInt(dest.getAttributeValue("x"));
        }
        if (dest.getAttribute("y") != null) {
            destY = Integer.parseInt(dest.getAttributeValue("y"));
        }
        if (destX > -1 && destY > -1) {
            destPos = new Point(destX, destY);
        }
        if (dest.getAttributeValue("z") != null) {
            destLevel = Integer.parseInt(dest.getAttributeValue("z"));
        }
        if (dest.getAttributeValue("map") != null) {
            destMapUID = (mapUID & 0xFFFF0000) + Integer.parseInt(dest.getAttributeValue("map"));
        }
        theme = dest.getAttributeValue("theme");
        sign = dest.getAttributeValue("sign");
    }

    if (dest != null) {
        d.portal.setDestination(destPos, destLevel, destMapUID);
    }
    d.portal.setDestTheme(theme);
    d.setSign(sign);
    return d;
}

From source file:neon.maps.MapLoader.java

License:Open Source License

private static Container loadContainer(Element container, String id, int x, int y, long itemUID, int mapUID) {
    Container cont = (Container) EntityFactory.getItem(id, x, y, itemUID);

    // lock difficulty
    if (container.getAttribute("lock") != null) {
        int lock = Integer.parseInt(container.getAttributeValue("lock"));
        cont.lock.setLockDC(lock);//from  w  w w . ja va 2s .c o  m
        cont.lock.setState(Lock.LOCKED);
    }
    // sleutel
    RItem key = null;
    if (container.getAttribute("key") != null) {
        key = (RItem) Engine.getResources().getResource(container.getAttributeValue("key"));
        cont.lock.setKey(key);
    }

    // trap
    int trap = 0;
    if (container.getAttribute("trap") != null) {
        trap = Integer.parseInt(container.getAttributeValue("trap"));
        cont.trap.setTrapDC(trap);
    }
    // spell
    if (container.getAttribute("spell") != null) {
        String spell = container.getAttributeValue("spell");
        RSpell.Enchantment enchantment = (RSpell.Enchantment) Engine.getResources().getResource(spell, "magic");
        cont.setMagicComponent(new Enchantment(enchantment, 0, cont.getUID()));
    }

    if (!container.getChildren("item").isEmpty()) { // indien items in map file
        for (Element e : container.getChildren("item")) {
            long contentUID = UIDStore.getObjectUID(mapUID, Integer.parseInt(e.getAttributeValue("uid")));
            Engine.getGame().getStore().addEntity(EntityFactory.getItem(e.getAttributeValue("id"), contentUID));
            cont.addItem(contentUID);
        }
    } else { // en anders default items
        for (String s : ((RItem.Container) cont.resource).contents) {
            Item i = EntityFactory.getItem(s, Engine.getGame().getStore().createNewEntityUID());
            Engine.getGame().getStore().addEntity(i);
            cont.addItem(i.getUID());
        }
    }

    return cont;
}