XML help...

Thanks for clicking on my question!

Basically I want to do find and insert on an existing xml file.

The following is the xml I work with:

<?xml version="1.0" encoding="UTF-8"?>

<cfg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="cfg.xsd">

<node ID="0">

<encode>asdf</encode>

<children>

<child>1</child>

</children>

</node>

<node ID="1">

<encode>asdf</encode>

<children>

<child>2</child>

</children>

</node>

<node ID="2">

<encode>asdf</encode>

<children>

<child>3</child>

</children>

</node>

<node ID="3">

<encode>asdf</encode>

<children>

<child>4</child>

</children>

</node>

I want to find a node with a specified attribute ID and insert a new node before the node that I found.

And my code is:

....

InputSource is =new InputSource(fileName);

Document doc = builder.parse(is);

//insert code

Element node = doc.createElement("node");

node.setAttribute("ID",id_tmp);

Element encode = doc.createElement("encode");

node.appendChild(encode);

Text encode_value = doc.createTextNode(encode_tmp);

encode.appendChild(encode_value);

Element children = doc.createElement("children");

node.appendChild(children);

Element child = doc.createElement("child");

children.appendChild(child);

child.appendChild(child_value);

Element root = doc.getDocumentElement();

Node t = doc.getElementById("3");

root.insertBefore(node, t);

The above code inserts a new node at the end instead of before the Node with ID="3".

What am I doing wrong here? Please help me fix this.

Thank you so much in advance.

James

Message was edited by:

kponenation

[2506 byte] By [kponenationa] at [2007-11-26 22:43:01]
# 1
fixed it...
kponenationa at 2007-7-10 11:59:01 > top of Java-index,Desktop,Developing for the Desktop...