搜索一下csdn的文档中心里关于xml的文章,有使用jdom的介绍,使用jdom可以生成和读取xml。

解决方案 »

  1.   

    用 jdom 
    示例如下:
    //从xml文件里读取
    SAXBuilder builder = new SAXBuilder();//初始化
    try {
        doc = builder.build(new File(path + XML_FILE));
    }
    catch (Exception e) {
        e.printStackTrace();
        return null;
    }String[] propName = new StringUtils().parsePropertyName(name);
    Element root = doc.getRootElement();//得到根元素
    for (int i = 0; i < propName.length; i++) {
         root = root.getChild(propName[i]);//得到propName[i]
         if (root == null) {
             System.out.println("this element has no children!");
    return null;//找不到,返回null
         }
    }//取得属性值
    String value = root.getText();
    if ("".equals(value)) {
        return null;//属性值为""返回null
    }
    else {
        //放入session
        value = value.trim();
        session.setAttribute(name, value);
        return value;
    }
      

  2.   

    NamedNodeMap map = null;
    NodeList regions = doc.getElementsByTagName("Region");
            for (int region = 0, regionNum = regions.getLength();
                                             region < regionNum; region++) {
                map = regions.item(region).getAttributes();
                for (int i = 0, iSize = map.getLength(); i < iSize; i++) {
                    int rowFrom = 0, rowTo = 0;
                    short colFrom = 0, colTo = 0;                rowFrom = Integer.parseInt(map.getNamedItem("rowFrom").
                                               getNodeValue());
                    rowTo = Integer.parseInt(map.getNamedItem("rowTo").getNodeValue());
                    colFrom = Short.parseShort(map.getNamedItem("colFrom").
                                               getNodeValue());
                    colTo = Short.parseShort(map.getNamedItem("colFrom").
                                             getNodeValue());
      

  3.   

    :)原来大家都在用Jdom 啊,,,帮忙顶
      

  4.   

    这篇文章或许对你有帮助,我刚刚看到的:http://dev.csdn.net/develop/article/14/14917.shtm
      

  5.   


                      用jdom.jar :
                      Document doc=null;
    SAXBuilder sbuilder = new SAXBuilder();
    Reader read = new StringReader(s);
    try {
    doc = sbuilder.build(read);
    }
    catch (JDOMException ex) {
    throw new JDOMException(ex);
    }

    Element root =  doc.getRootElement();
    Element varElement = root.getChild("Root");
    Iterator it = varElement.getChildren().iterator();
    while(it.hasNext())
    {
       .................
    }
      

  6.   

    只是读取的话,用SAX就可以了
     public void startElement(String namespaceURI,
                               String lName,
                               String qName,
                               Attributes attrs) throws SAXException {
        String eName = lName;
        if ("".equals(eName))
          eName = qName;
        lastElement = eName;
        if (attrs != null) {
          for (int i = 0; i < attrs.getLength(); i++) {
            String aName = attrs.getLocalName(i);
            if ("".equals(aName))
              aName = attrs.getQName(i);
            if (aName.equals("about") || aName.equals("rdf:about")) //只把about属性加入结果集
              add2Map(aName, attrs.getValue(i));
          }    }
      }  public void endElement(String namespaceURI,
                             String sName,
                             String qName) throws SAXException {    if (sName.equals("Description") || qName.equals("rdf:Description")) {
          Object value = tags.get("label");
          if (value != null) {
            ArrayList temp = (ArrayList) value;
            for (int i = 0; i < temp.size(); i++) {
              if ( ( (String) temp.get(i)).toLowerCase().indexOf(searchKey) != -1) {
                resultArray.add(tags);          }
            }
          }
          tags = null;
        }  }
      

  7.   

    还 有个问题啊
    如何将XML文件,转化为Document,谢谢
      

  8.   

    比如:
    DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
    DocumentBuilder builder=factory.newDocumentBuilder();
    Document doc=builder.parse(fileName);