zoukankan      html  css  js  c++  java
  • 解析XML

    import java.util.HashMap;
    import java.util.Map;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;

    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;


    public class Test {

    public static void main(String[] args) {
    Test t=new Test();
    String xmlPath = "src/pay.xml";
    Map<String, Object> map=t.getFamilyMemebers(xmlPath);
    System.out.println(map.get("appid"));
    }

    public Map<String, Object> getFamilyMemebers(String xmlPath){
    Map<String, Object> map=new HashMap<String, Object>();
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    try {
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(xmlPath); // 使用dom解析xml文件

    NodeList sonlist = doc.getElementsByTagName("WeChat");
    for (int i = 0; i < sonlist.getLength(); i++) // 循环处理对象
    {
    Element son = (Element)sonlist.item(i);;

    for (Node node = son.getFirstChild(); node != null; node = node.getNextSibling()){
    if (node.getNodeType() == Node.ELEMENT_NODE){
    String name = node.getNodeName();
    String value = node.getFirstChild().getNodeValue();
    // System.out.println(name+" : "+value);
    map.put(name, value);
    }
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return map;
    }
    }

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><father>
    <Alipay></Alipay>
    <WeChat>
    <appid>a</appid>
    <mchid>b</mchid>
    <partnerkey>c</partnerkey>
    <notifyurl>d</notifyurl>
    </WeChat>
    </father>

  • 相关阅读:
    hdu 1425 sort 解题报告
    codeforces B. Jeff and Periods 解题报告
    codeforces A. Jeff and Digits 解题报告
    codeforces B. Xenia and Spies 解题报告
    Python 条件判断的使用
    Python Apache日志处理脚本(初稿)
    Python函数定义
    Python多条件配合使用
    Python循环的使用(2)
    NavigationController导航栏中添加多个UIBarButtonItem
  • 原文地址:https://www.cnblogs.com/lanliying/p/4826263.html
Copyright © 2011-2022 走看看