【问题标题】:Remove entire xml node [closed]删除整个 xml 节点 [关闭]
【发布时间】:2014-01-09 17:49:43
【问题描述】:

我尝试使用 jdom2 从我的 xml 中删除一个节点,但我没有找到答案

我的 xml 是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<banque>
  <compte idCompte="5646">
    <numCompte>5646</numCompte>
    <nom>Ludo</nom>
    <solde>850</solde>
  </compte>
  <compte idCompte="4546">
    <numCompte>4546</numCompte>
    <nom>Antoine</nom>
    <solde>9999.4</solde>
  </compte>

我认为我需要定位属性 idCompte 并将其删除,但我不知道该怎么做。

如果你有一个(简单的)解决方案,我很高兴看到 :)

【问题讨论】:

  • 您希望 jdom2 代码删除节点 compte 的 idComplete 属性??

标签: java xml sax jdom jdom-2


【解决方案1】:
What you are trying to achieve is complex(multi-step process) in nature,
I can share with you the steps(algorithm) that you need to follow:

Step:1
You need to genearte the schema first:
Generate the schema from this online tool: 
http://www.xmlforasp.net/CodeBank/System_Xml_Schema/BuildSchema/BuildXMLSchema.aspx

Step:2
Using these schema generate the Java classes:
using api tools like XMLBeans or JAXB
a) scomp -out employeeschema.jar employeeschema.xsd (Ref: http://xmlbeans.apache.org/)
b) xjc -d out customer.xsd (Ref: http://blog.bdoughan.com/2011/10/jaxb-xjc-imported-schemas-and-xml.html)

Step:3 
Then follow below tutorial for your reference for in-depth CRUD operation that you want to perform on your XML:
Jaxb: http://www.mkyong.com/java/jaxb-hello-world-example/
Xstream: http://x-stream.github.io/tutorial.html
XMLBeans: http://xmlbeans.apache.org/documentation/tutorial_getstarted.html

【讨论】:

  • 感谢您的步骤 :) 不过我已经使用 Xpath 在 stackoverflow 中找到了解决方案,如果我不重新找到它,我喜欢这篇文章或我的 java。
猜你喜欢
  • 1970-01-01
  • 2011-05-30
  • 1970-01-01
  • 1970-01-01
  • 2016-06-26
  • 2011-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多