简体   繁体   中英

get Element by ID using DOM parser in JAVA for another XML structure

I have an XML file which is structured like that:

    <Users>
<user id="1" groupid="G1" username="GON"/>
<user id="2" groupid="G2" username="NARUTO"/>
<user id="3" groupid="G3" username="GOKU"/>
</Users>

I need to getAttribute value of groupid and username

For now my JAVA CODE ;

    Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("Users");
for (int i = 0; i < nodes.getLength(); i++) {
    Element element = (Element) nodes.item(i);          
    NodeList Users = element.getElementsByTagName("User");
    for (int temp = 0; temp < Users.getLength(); temp++) {
        Node nNode = Users.item(temp);                                      
        System.out.println("\nCurrent Element :" + nNode.getNodeName());
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            System.out.println("Staff username : " + eElement.getAttribute("username"));
        }
    }      
}

you can use xstream to do this , and write your own Converter:

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.DomDriver;

import javax.xml.bind.JAXBException;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author heyunxia (love3400wind@163.com)
 * @version 1.0
 * @since 2016-01-15 上午11:21
 */
public class Xml2Bean {

    public static void main(String[] args) throws JAXBException, FileNotFoundException {

        XStream xstream = new XStream(new DomDriver());
        xstream.registerConverter(new UserCoverter());
        xstream.processAnnotations(User.class);
        xstream.addImplicitCollection(Users.class, "list");
        xstream.autodetectAnnotations(true);


        List<User> list = new ArrayList<>();
        list.add(new User("1", "G1", "GON"));
        list.add(new User("2", "G2", "NARUTO"));
        list.add(new User("3", "G3", "GOKU"));
        Users users = new Users(list);

        String xml = xstream.toXML(users);
        System.out.println(xml);

        Users usersFile = (Users) xstream.fromXML(xml);
        System.out.printf(usersFile.toString());
    }
}

class UserCoverter implements Converter {

    @Override
    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
        User user = (User) source;
        writer.addAttribute("id", user.getId());
        writer.addAttribute("groupid", user.getGroupid());
        writer.addAttribute("username", user.getUsername());

    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        User user = new User();
        user.setId(reader.getAttribute("id"));
        user.setGroupid(reader.getAttribute("groupid"));
        user.setUsername(reader.getAttribute("username"));
        return user;
    }

    @Override
    public boolean canConvert(Class type) {
        return type.equals(User.class);
    }
}

@XStreamAlias("Users")
class Users {
    private List<User> list;

    public Users(List<User> list) {
        this.list = list;
    }
}


@XStreamAlias("user")
class User {
    @XStreamAlias("id")
    private String id;
    private String groupid;
    private String username;

    public User() {
    }

    public User(String id, String groupid, String username) {
        this.id = id;
        this.groupid = groupid;
        this.username = username;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getGroupid() {
        return groupid;
    }

    public void setGroupid(String groupid) {
        this.groupid = groupid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM