首页 \ 问答 \ 无法使用jdom创建xml文件(can not create the xml file using jdom)

无法使用jdom创建xml文件(can not create the xml file using jdom)

这是我的代码:

import java.io.FileWriter; 
import java.io.IOException; 
import org.jdom2.Attribute; 
import org.jdom2.Document; 
import org.jdom2.Element; 
import org.jdom2.output.Format; 
import org.jdom2.output.XMLOutputter;


try {
    Element FICHADAS = new Element("FICHADAS");
    Document doc = new Document(FICHADAS);
    doc.setRootElement(FICHADAS);
    Element fichada = new Element("fichada");
    fichada.addContent(new Element("N_Terminal").setText("XX"));
    fichada.addContent(new Element("Tarjeta").setText("XX"));
    fichada.addContent(new Element("Fecha").setText("XX"));
    fichada.addContent(new Element("Hora").setText("XX"));
    fichada.addContent(new Element("Causa").setText("XX"));
    doc.getRootElement().addContent(fichada);
    XMLOutputter xmlOutput = new XMLOutputter();
    xmlOutput.setFormat(Format.getPrettyFormat());
    xmlOutput.output(doc, new FileWriter("c:\file.xml"));
} catch(IOException e) {

}

我试图在C:\中找到file.xml,但不在这里,我不知道为什么,控制台告诉我:元素“FICHADAS”无法添加为文档的根目录:内容已经有一个现有的父文件

// NEW我在想,现在我只需要将新的fichadas添加到现有文档中,不需要每次打开程序时都创建它。


Here is my code:

import java.io.FileWriter; 
import java.io.IOException; 
import org.jdom2.Attribute; 
import org.jdom2.Document; 
import org.jdom2.Element; 
import org.jdom2.output.Format; 
import org.jdom2.output.XMLOutputter;


try {
    Element FICHADAS = new Element("FICHADAS");
    Document doc = new Document(FICHADAS);
    doc.setRootElement(FICHADAS);
    Element fichada = new Element("fichada");
    fichada.addContent(new Element("N_Terminal").setText("XX"));
    fichada.addContent(new Element("Tarjeta").setText("XX"));
    fichada.addContent(new Element("Fecha").setText("XX"));
    fichada.addContent(new Element("Hora").setText("XX"));
    fichada.addContent(new Element("Causa").setText("XX"));
    doc.getRootElement().addContent(fichada);
    XMLOutputter xmlOutput = new XMLOutputter();
    xmlOutput.setFormat(Format.getPrettyFormat());
    xmlOutput.output(doc, new FileWriter("c:\file.xml"));
} catch(IOException e) {

}

i try to find the file.xml in C:\ but is not here and I don't know why, and the console show me that: The element "FICHADAS" could not be added as the root of the document: The Content already has an existing parent document

//NEW I was thinking, and now i only need to add the new fichadas to the existing document, not need to create it every time that i opened the program.


原文:https://stackoverflow.com/questions/29790834
更新时间:2021-11-15 09:11

最满意答案

如果您之前没有任何Java经验,这可能是一项艰巨的任务。

您首先需要建立调用图的格式。 然后使用单词边界,正则表达式或解析器生成器(如antlr)进行标记。

除此之外,问题对于StackOverflow上的完整答案来说太大了。 尝试研究这些选项,并在遇到问题时询问您选择的选项。


If you don't have any previous experience in Java this may be a tall order.

You will first need to establish the format of the call graph. Then tokenize either using word boundaries, regular expression or a parser generator such as antlr.

Beyond that, the problem is too large for a complete answer on StackOverflow. Try to study these options and ask specific questions the option you choose when you encounter problems.

相关问答

更多
  • 我没有看到乔达认识到这是一个有效的日期。 例: strict = org.joda.time.format.DateTimeFormat.forPattern("MM/dd/yyyy") try { strict.parseDateTime('40/40/4353') assert false } catch (org.joda.time.IllegalFieldValueException e) { assert 'Cannot parse "40/40/4353": Value ...
  • 这些是更多的解析技术,而不是实际的解析工具。 Xerces是解析工具的一个示例。 SAX有助于提高速度和解析事件处理。 从事件中解析可以轻松地让您跳过无趣的事件,并且跳过的惩罚很低。 在读取整个文档时,无需存储它,因此SAX可以处理大于系统内存的文档。 DOM适合将整个文档保存在内存中。 这样做很容易让你工作需要文档的所有元素(交叉引用等)。 这种便利是有代价的,与SAX不同,您必须有足够的内存来容纳整个文档。 JDOM基本上是DOM,但具有不同的编程接口。 它假设它的编程接口更符合Java,无论这意味着什 ...
  • 源码分析是程序员离不开的话题。无论是研究开源项目,还是平时做各类移植、开发,都避免不了对源码的深入解读。   传统的命令行工具 Cscope, Ctags 可以结合 vim 等工具提供高效快捷的跳转,但是无法清晰的展示函数内部的逻辑关系。   至于图形化的IDE,如 QtCreator, Source Insight, Eclipse, Android Studio 等,却显得笨重,而且不一定支持导出调用关系图。
  • 只需使用JavaCC。 看一下本教程 ,开始吧。 学习曲线需要几个小时的时间,但从长远来看,你会很高兴。 Just use JavaCC. Take a look at this tutorial to get you started. The learning curve will require a couple of hours of your time but you'll be glad you did in the long run.
  • 我在Ubuntu 15.10上遇到了同样的问题,我在这里找到了解决方案: https : //bugs.launchpad.net/ubuntu/+source/linux/+bug/1396654 这里是步骤: sudo apt-get install libiberty-dev binutils-dev mkdir ~/install cd ~/install apt-get source linux-tools-`uname -r` sudo apt-get build-dep linux-tools ...
  • 见http://kenai.com/projects/jruby-parser/ See http://kenai.com/projects/jruby-parser/
  • 你认为这是一个好主意我直接在java中调用mysql解析器还是可能的? 不,我认为这不是一个好主意。 从Java调用C / C ++代码很少是个好主意。 在这种情况下,解析器嵌入在一大堆其他代码中,并且很可能将其分离出来。 最重要的是,它将发出解析树作为难以从Java使用的复杂本机数据结构。 一种更好的方法是为SQL提供现有的开源Java解析器,并且(随着时间的推移)添加对解析MySQL特定语句的支持。 Do you think that is a good idea I call the mysql pa ...
  • 我不知道是否有一个很好的开箱即用的选择,给出几乎无限数量的排列; 但是如果我必须这样做,我会从事实上的Java日期包Joda开始 ,只是使用允许的数据格式序列进行线性查找,并接受第一个可以解析的数据格式。 I don't know if there is a good out-of-the-box choice, given almost unlimited number of permutations; but if I had to do it, I'd start with de-facto Java ...
  • 如果您之前没有任何Java经验,这可能是一项艰巨的任务。 您首先需要建立调用图的格式。 然后使用单词边界,正则表达式或解析器生成器(如antlr)进行标记。 除此之外,问题对于StackOverflow上的完整答案来说太大了。 尝试研究这些选项,并在遇到问题时询问您选择的选项。 If you don't have any previous experience in Java this may be a tall order. You will first need to establish the for ...
  • kcachegrind显示可能会令人困惑。 您看到的“三个数字”实际上是一个数字,分成三个数字的组。 想象一下逗号或点,取决于您在数字组之间的区域设置。 产生的一个数字是成本(计算在程序中观察程序的次数)。 带有蓝色填充的框是“百分比”,因此除了功能的绝对成本之外,您还可以粗略估计为此功能分配的总成本的百分比。 PS:我不知道尾随的'x'是什么意思。 也许其他人可以帮助那个! The kcachegrind display can be confusing. The "three numbers" you ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。