我刚学会了AJAX调用servlet
我看到别人有的项目中,使用XML文件做数据源,使用java程序读取XML文件里的内容,然后在JSP页面展现出来:这个是怎么实现的啊,谁能提供点源码吗,感激不尽!
还有AJAX中说的DOM模型,跟DOM解析XML有什么区别啊问的问题有点多,可是分不多了,别生气啊,帮帮忙好吗

解决方案 »

  1.   

    /**
     * 建立数据文件的读取连接
     * 
     * @return 读取成功时返回Document对象,失败则返回空
     */
    public static Document getDocument() { Document doc1 = null;
    ConfigManager xmlConfig = new ConfigManager(
    "./servConfig/xmlConfig.ini");
    File file = new File(xmlConfig.getValue("xml_path"));
    SAXReader xml_read = new SAXReader();
    try {
    doc1 = xml_read.read(file);
    } catch (DocumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
    }
    return doc1; } /**
     * 把数据写到XML文件的方法
     * 
     * @param doc
     *            需要写入数据的XML文件的Document
     */
    private void output_xml(Document doc) { ConfigManager xmlConfig = new ConfigManager(
    "./servConfig/xmlConfig.ini");
    File file = new File(xmlConfig.getValue("xml_path"));
    OutputFormat op_format = OutputFormat.createPrettyPrint();
    op_format.setEncoding("GBK");
    op_format.setIndent("\t");
    try {
    XMLWriter xml_writer = new XMLWriter(new FileOutputStream(file),
    op_format);
    xml_writer.write(doc); } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }/**
     * 查找用户数据,
     * 
     * @param doc
     *            要查找的数据文件
     * @param id
     *            通过用户id条件精确查询
     * 
     */
    private Element search_user(Document doc, String id) { Element root = doc.getRootElement();
    String xml_path = "/users/user[@sid='" + id + "']";
    Node node = root.selectSingleNode(xml_path);
    if (node != null) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
    return (Element) node;
    }
    } return null;
    }三个方法够了吧。
      

  2.   

    把你的xml文件永成map 一个一个map包传送过去
      

  3.   

    楼主参考一下我的这个博客
    “java读取xml配置文件(小结)”
    希望对你有帮助
      

  4.   

    首先你可以参考下面的网址,学习下如果Java解析xml
    http://www.it.com.cn/f/edu/053/27/93819.htm
    然后,关于ajax解析dom,dom是个模型概念。dom包括xml,所以会这个,等于前者就没必要了。当然你可以不要管他的原理。
      

  5.   

    java 四种操作XMLhttp://www.lifeispig.cn/article.asp?id=94
      

  6.   

    可能要根据你的xml文件作适当修改,
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    public class DomParserTest {
    public static void main(String[] args) throws Exception{
    DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
    DocumentBuilder builder=factory.newDocumentBuilder();
    Document doc=builder.parse("d:\\test01.xml");
    Element root=doc.getDocumentElement();
    printElement(root);
    }

    public static void printElement(Element e){
    System.out.print("<"+e.getTagName());
    NamedNodeMap map=e.getAttributes();
    for(int i=0;i<map.getLength();i++){
    Attr att=(Attr)map.item(i);
    System.out.print(" "+att.getName()+"=\""+att.getValue()+"\" ");
    }
    System.out.println(">");
    NodeList list=e.getChildNodes();
    for(int i=0;i<list.getLength();i++){
    Node n=list.item(i);
    if(n.getNodeType()==Node.ELEMENT_NODE){
    Element temp=(Element)n;
    printElement(temp);
    }else{
    System.out.println(n.getTextContent().trim());
    }
    }

    System.out.println("</"+e.getTagName()+">");

    }}