java如何改变dom元素,Java DOM - 插入一个元素,又一个

Given the following XML file:

name="TestSVG2"

xmlns="http://www.example.org"

targetNamespace="http://www.example.org"

xmlns:xsd="http://www.w3.org/2001/XMLSchema">

I want to add a new element inside the after a certain pre-existing element. For example if I want to add the node after "Assign1", the new XML should like this:

I have to do this by using Java DOM, in a function. The function signature should like this:

public void addActionDom(String name, String stepType, String stepName)

Where:

name is the pre-existing element, after which the insertion will be made;

stepType is the inserted element type;

stepName is the name attribute of the newly inserted element.

Currently I am lacking experience with JDOM, or any other Java XML library. Can you please give a sample code, or point me to a tutorial where an insertion after a certain element is made.

This is the code I have until now:

public void addActionDom(String name, String stepType, String stepName) {

File xmlFile = new File(path + "/resources/" + BPELFilename);

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db;

try {

/* Load XML */

db = dbf.newDocumentBuilder();

Document doc = db.parse(xmlFile);

doc.getDocumentElement().normalize();

/* Iterate throughout the type tags and delete */

for (String cTag : typeTags) {

NodeList cnl = doc.getElementsByTagName(cTag);

for (int i = 0; i < cnl.getLength(); ++i) {

Node cnode = cnl.item(i);

if (cnode.getNodeType() == Node.ELEMENT_NODE) {

Element elem = (Element)cnode; // 'elem' Element after which the insertion should be made

if (elem.getAttribute("name").equals(name)) {

Element newElement = doc.createElement(stepType); // Element to be inserted

newElement.setAttribute("name", stepName);

// CODE HERE

}

}

}

}

/* Save the editing */

Transformer transformer =

TransformerFactory.newInstance().newTransformer();

StreamResult result =

new StreamResult(new FileOutputStream(path + "/resources/" +

BPELFilename));

DOMSource source = new DOMSource(doc);

transformer.transform(source, result);

} catch (Exception e) {

/* ParserConfigurationException */

/* SAXException */

/* IOException */

/* TransformerConfigurationException */

/* TransformerException */

/* Exception */

e.printStackTrace();

}

}

}

解决方案

Ok, Aaron Digulla beat me regarding speed. Had to figure it out myself as well.

I didnt use cnl.item(i+1) but nextSibling():

Element newElement = doc.createElement(stepType); // Element to be inserted

newElement.setAttribute("name", stepName);

elem.getParentNode().insertBefore(newElement, elem.getNextSibling());

You cannot insert Nodes at a specified index. The only node-inserting methods are

appendChild(Node node) //appends the given child to the end of the list of children

and

insertBefore(Node new, Node child) //inserts "new" into the list, before the 'child' node.

If there was a insertAfter(Node new, Node child) method, this would be very easy for you. But there isn't, unfortunately.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值