import javax.xml.parsers.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.entity.vo.EmailVo;
@Controller
@Scope("prototype")
public class ReadXml {
private String name;
private String pass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
//这是dom解析
public EmailVo parserXml(String fileName , String level) {
try {
EmailVo emailVo = new EmailVo();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(fileName);
NodeList employees = document.getChildNodes();
for (int i = 0; i < employees.getLength(); i++) {
Node employee = employees.item(i);
NodeList employeeInfo = employee.getChildNodes();
for (int j = 0; j < employeeInfo.getLength(); j++) {
Node node = employeeInfo.item(j);
NodeList employeeMeta = node.getChildNodes();
for (int k = 0; k < employeeMeta.getLength(); k++) {
if(employeeMeta.item(1).getTextContent().equalsIgnoreCase(level)){
emailVo.setLevel(employeeMeta.item(1).getTextContent());
if(employeeMeta.item(k).getNodeName().equals("name")){
System.out.println(employeeMeta.item(k).getTextContent());
emailVo.setEmailName(employeeMeta.item(k).getTextContent());
}
if(employeeMeta.item(k).getNodeName().equals("password")){
System.out.println(employeeMeta.item(k).getTextContent());
emailVo.setPassword(employeeMeta.item(k).getTextContent());
}
}
}
}
}
System.out.println("解析完毕");
return emailVo;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
public static void main(String[] args) {
ReadXml readXml = new ReadXml();
System.out.println(readXml.parserXml("WebRoot/email/emailN&P.xml","1").getPassword());
}
/*完全解析
* try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(fileName);
NodeList employees = document.getChildNodes();
List<EmailVo> list = new ArrayList<EmailVo>();
EmailVo emailVo = null;
for (int i = 0; i < employees.getLength(); i++) {
Node employee = employees.item(i);
NodeList employeeInfo = employee.getChildNodes();
for (int j = 0; j < employeeInfo.getLength(); j++) {
Node node = employeeInfo.item(j);
NodeList employeeMeta = node.getChildNodes();
emailVo = new EmailVo();
for (int k = 0; k < employeeMeta.getLength(); k++) {
// if(employeeMeta.item(1).getTextContent().equalsIgnoreCase(level)){
if(employeeMeta.item(k).getNodeName().equals("name")){
emailVo.setLevel(employeeMeta.item(k).getTextContent());
System.out.println("name ==== "+employeeMeta.item(k).getTextContent());
emailVo.setEmailName(employeeMeta.item(k).getTextContent());
}
if(employeeMeta.item(k).getNodeName().equals("password")){
System.out.println("password ==== "+employeeMeta.item(k).getTextContent());
emailVo.setPassword(employeeMeta.item(k).getTextContent());
}
// }
}
if (emailVo.getLevel() != null) {
list.add(emailVo);
}
}
}
System.out.println("解析完毕");
return list;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
*
* **/
}