能不能像操作数据库那样读写xml文件?

解决方案 »

  1.   

    看看api里javax.xml这部分
    要用到DocumentBuilderFactory,DocumentBuilder这些
      

  2.   

    JDK 1.5 中还增加了 XPath,用于查询数据很方便的。
      

  3.   

    可以用apche的类库,我前两天正好在公司写了个操作xml文件的类,给你看下:
    ..............import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.input.SAXBuilder;
    import org.jdom.output.Format;
    import org.jdom.output.XMLOutputter;
    import org.xml.sax.InputSource;...........        DomainModel domainModel = (DomainModel) request.getSession().getAttribute("TABLE");
            InputSource is = new InputSource("d:\\huaxia\\huaxiaweb\\src\\main\\resources\\myapp.struts.xml");
            is.setEncoding("UTF-8");
            SAXBuilder sb = new SAXBuilder();
            Document structsXML;
            try {
                structsXML = sb.build(is);
            } catch (JDOMException e1) {
                // TODO Auto-generated catch block
                logger.info(e1.getMessage());
                return;
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                logger.info(e1.getMessage());
                return;
            }        // StructsXML文件的根元素
            Element root = structsXML.getRootElement();        Element actionMapping = root.getChild("action-mappings");
            
            boolean haveMainAction = false;
            boolean haveUpdateAction = false;
            boolean haveCreateAction = false;
            java.util.List<Element> elements = actionMapping.getChildren();
            for(Element el: elements){
                if( el.getAttributeValue("path").equals( ("/" + domainModel.getTableNameInJava() + "MainAction"))
                    && el.getAttributeValue("parameter").equals("method")
                    && el.getAttributeValue("name").equals((domainModel.getTableNameInJavaFirstLow() + "Form"))){
                    logger.info(domainModel.getTableNameInJava() +"MainAction has already existed.");
                    haveMainAction = true;
                }
                if( el.getAttributeValue("path").equals( ("/" + domainModel.getTableNameInJava() + "CreateAction"))
                    && el.getAttributeValue("input").equals( ("/" + domainModel.getTableNameInJava() + "Create.jsp"))){
                    logger.info(domainModel.getTableNameInJava() + "CreateAction has already existed.");
                    haveCreateAction = true;
                }
                if( el.getAttributeValue("path").equals( ("/" + domainModel.getTableNameInJava() + "UpdateAction"))
                    && el.getAttributeValue("input").equals( ("/" + domainModel.getTableNameInJava() + "Update.jsp"))){
                    logger.info(domainModel.getTableNameInJava() + "UpdateAction has already existed.");
                    haveUpdateAction = true;
                }
            }
            
       
      

  4.   

    我解释下:
     InputSource is = new InputSource("d:\\huaxia\\huaxiaweb\\src\\main\\resources\\myapp.struts.xml"); 
    把一个xml文件读入InputSource类  这个类是jdom的一个部分。
                   is.setEncoding("UTF-8"); 
            SAXBuilder sb = new SAXBuilder(); 
            Document structsXML; 
    try { 
                structsXML = sb.build(is); 
            } catch (JDOMException e1) { 
                // TODO Auto-generated catch block 
                logger.info(e1.getMessage()); 
                return; 
            } catch (IOException e1) { 
                // TODO Auto-generated catch block 
                logger.info(e1.getMessage()); 
                return; 
            } 
    把这个文件和一个Document类关联起来得到这个Document的根元素
            Element root = structsXML.getRootElement(); 
    根元素的所有二级元素, 放在list中
    java.util.List <Element> elements = actionMapping.getChildren(); 然后通过element.setAttributes等等方法来修改,
    最后记得把这个document重新写入io流放入xml文件即可,
    上google搜索jdom 有文档下载 英文
      

  5.   

    你可以读xml文件,从xml的属性来取得xml中的数据
    f = new File(SavingFilePath,SavingFileName);//你的xml的路径名和文件名
    DocumentBuilderFactory factory=DocumentBuilderfactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(f);
    String str = doc.getElementsByTagName(属性).item(num).getFirstChild().getNodeValue();
    //num=那个属性排在在第几就是几.
    这样就可以读到xml的文件了...你就可以操作XML文件..
      

  6.   

    用java自带的函数操作方便吗
    还是用第三方的包进行操作方便呢?如dom4j ,JDOM 等
      

  7.   

    不能用xm来替代数据库
    小量的存储是可以的。
    最好的用途是在通信,数据交互上。xpath查询语言可以很方便的查询xml,但是由于xml和关系数据库有明显区别,不要期望xpath能和t-sql一样
      

  8.   

    package run;
    import org.jdom.*;
    import org.jdom.output.*;
    import java.io.*;
    public class WriteXML{
        public void BuildXML() throws Exception {
            Element root,student,number,name,age,firstname,lastname;         
            root = new Element("student-info"); //生成根元素:student-info
            student = new Element("student"); //生成元素:student(number,name,age)                             
            number = new Element("number");
            name = new Element("name");
            age = new Element("age");
            firstname = new Element("firstname");
            lastname = new Element("lastname");
            Document doc = new Document(root); //将根元素植入文档doc中
            root.setText("root_name");
            number.setText("001");
            firstname.setText("yu");
            lastname.setText("haiming");
            age.setText("24");
            student.addContent(number);
            student.addContent(name);
            student.addContent(age);
            name.addContent(firstname);
            name.addContent(lastname);
            root.addContent(student);
            
            root.setAttribute("width", "100%");
            root.setAttribute("height", "860px");
            root.setAttribute("bgcolor", "red");
            root.setAttribute("name", "student");
            Format format = Format.getCompactFormat();
            format.setEncoding("gb2312"); //设置xml文件的字符为gb2312
            format.setIndent("    "); //设置xml文件的缩进为4个空格
            XMLOutputter XMLOut = new XMLOutputter(format);//元素后换行一层元素缩四格 
            XMLOut.output(doc, new FileOutputStream("studentinfo.xml"));  
    }
        public static void main(String[] args) throws Exception {
            WriteXML w = new WriteXML();
            System.out.println("Now we build an XML document .....");
            w.BuildXML();
            System.out.println("finished!");
    }
    }