我有一个XML文件,结构如下:
<Users>
<user id="1" groupid="G1" username="GON"/>
<user id="2" groupid="G2" username="NARUTO"/>
<user id="3" groupid="G3" username="GOKU"/>
</Users>
我需要getAttribute groupid和username的值
现在我的JAVA代码;
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"));
}
}
}
答案 0 :(得分:0)
您可以使用xstream执行此操作,并编写自己的转换器:
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;
}
}