首页 \ 问答 \ 通过JDOM向XML添加内容(add contant to XML through JDOM)

通过JDOM向XML添加内容(add contant to XML through JDOM)

我一直在开始研究JDOM。 到目前为止,我已经创建了一个名为file.xml的文件。 现在我想在XML文件中添加一些内容,但我有点不安全如何做到这一点? 那可能是: - 名字 - 姓氏 - 年龄

希望可以有人帮帮我? 来自朱莉的最好问候

    ppackage examplePackage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class ReadXMLFile {

    public static void main(String[] args) {
        try {
            write();
            read();
        }
        catch (FileNotFoundException e){
            e.printStackTrace();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
        catch(JDOMException e) {
            e.printStackTrace();
        }
    }

        public static void read() throws JDOMException, IOException {


            SAXBuilder reader = new SAXBuilder();
            Document document = reader.build(new File("file.xml"));
            XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
            xout.output(document, System.out);
        }

        public static void write() throws FileNotFoundException, IOException {



Document document = new Document();
        Element root = new Element("document");
        root.setAttribute("file", "file.xml");
        root.addContent(new Element("style"));
        document.setRootElement(root);

        Element person = new Element("Person");             
        person.setAttribute("name", "Mads");
        }
}

在控制台中打印:

<?xml version="1.0" encoding="UTF-8"?>
<document file="file.xml">
  <style />
</document>

在这种情况下,它应该打印出名为“Mads”的人吗?


I have been starting to look at JDOM. Until now I have created a file which has the name file.xml. Now I would like to add some content in the XML file, but I am a little bit insecure how to do that? That could fx be: - name - lastname - age

Hope someone can help me? Best Regards from Julie

    ppackage examplePackage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class ReadXMLFile {

    public static void main(String[] args) {
        try {
            write();
            read();
        }
        catch (FileNotFoundException e){
            e.printStackTrace();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
        catch(JDOMException e) {
            e.printStackTrace();
        }
    }

        public static void read() throws JDOMException, IOException {


            SAXBuilder reader = new SAXBuilder();
            Document document = reader.build(new File("file.xml"));
            XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
            xout.output(document, System.out);
        }

        public static void write() throws FileNotFoundException, IOException {



Document document = new Document();
        Element root = new Element("document");
        root.setAttribute("file", "file.xml");
        root.addContent(new Element("style"));
        document.setRootElement(root);

        Element person = new Element("Person");             
        person.setAttribute("name", "Mads");
        }
}

Print out in console:

<?xml version="1.0" encoding="UTF-8"?>
<document file="file.xml">
  <style />
</document>

In this case it should print out the person with the name "Mads" right?


原文:https://stackoverflow.com/questions/22606679
更新时间:2023-06-27 08:06

最满意答案

我在onDomRefresh()函数的开头添加了这个调用,该函数取消绑定元素上的所有事件:

this.$el.unbind();

现在,点击事件在重新渲染视图后仅触发一次。


I have added this call at the beggining of onDomRefresh() function, which unbinds all the events on the element:

this.$el.unbind();

Now, the click event fires just once after rerendering the view.

相关问答

更多
  • 这是我要做的, 使用CollectionView作为菜单 使用CollectionView为您的菜单提供动态定义菜单的功能,您的菜单将使用一组模型定义,如下所示 { lable : 'Home', href : '/home', icon : 'pic/home.png' } 让控制器更新菜单 我假设您的控制器在触发路由时更新内容区域。 您可以让控制器使用MenuItemCollection重新渲染菜单,该菜单包含与正在显示的内容相关的菜单项列表。 Here is what I would ...
  • 你缺少div的css类,它应该作为模态对话框: you are missing css classes for div, which should act as modal dialog:
  • 你并没有'要求'Marionette(或者访问它的完全限定名称Backbone.Marionette) You aren't 'requiring' Marionette (or alternatively accessing it's fully qualified name Backbone.Marionette)
  • 对于诸如此类的情况,您应该利用onDestroy功能来执行Marionette提供的额外清理工作。 更换或删除视图时,Marionette会自动调用onDestroy 。 onDestroy: function() { App.vent.off('ButtonClicked', this.onButtonClicked, this); } 从木偶文件: 通过在视图定义中提供onDestroy方法,您可以为视图运行自定义代码,该视图在视图被销毁和清理后触发。 onDestroy方法将传递任何用 ...
  • 更改Backbone shim以在导出中大写Backbone : backbone: { deps : ['jQuery', 'lodash'], exports : 'Backbone' }, 因为Backbone将自己定义为Backbone而不是backbone 。 其他地方可能存在问题,但您必须这样做。 Change you Backbone shim to capitalize Backbone in the exports: backbone: { ...
  • 基本上木偶为你做了一切,你只需要正确地初始化集合视图。 您可以指定集合视图应该侦听哪些子视图事件(默认情况下它会侦听某些默认事件) 以下是子视图的搜索功能和事件处理示例: HTML