带节点的dom xml解析器

时间:2012-02-01 17:38:04

标签: java xml dom

我有这个XML文档,我想解析它,我这样做是为了解析它但是我在函数public method getFunction中遇到了一个问题,我希望将我的元素放入一个字符串数组中。任何人都可以说出问题是什么

public void parseXmlFile() {
    Document ret = null;
    DocumentBuilderFactory domFactory;
    DocumentBuilder builder;
    ArrayList myFunc  = new ArrayList();


    try {
        domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setValidating(false);
        domFactory.setNamespaceAware(false);
        builder = domFactory.newDocumentBuilder();
        ret = builder.parse("test.xml");
                }                           
    catch (Exception ex) {
        System.err.println("unable to load XML: " + ex);
    }

    Element docEle = ret.getDocumentElement();

    NodeList nl = docEle.getElementsByTagName("Function");
    if(nl != null && nl.getLength() > 0) {
        for(int i = 0 ; i < nl.getLength();i++) {               
            Element el = (Element)nl.item(i);
            Function e = getFunction(el);
            myFunc.add(e);
        }
    }
}

   public Function getFunction (Element empEl) {


    String used[] = getTextValue(empEl,"UsedFunctions");
    String type = empEl.getAttribute("type");

    Function e = new Function(used[] ,type);

    return e;
}

   public  String getTextValue(Element ele, String tagName) {
    String textVal = null;
    NodeList nl = ele.getElementsByTagName(tagName);
    if(nl != null && nl.getLength() > 0) {
        Element el = (Element)nl.item(0);
        textVal = el.getFirstChild().getNodeValue();
    }

    return textVal;
}

0 个答案:

没有答案