Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] Element.insertChild(Node, int)

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Haig Ehramdjian <haige AT cae.com>
  • To: "'xom-interest AT lists.ibiblio.org'" <xom-interest AT lists.ibiblio.org>
  • Subject: [XOM-interest] Element.insertChild(Node, int)
  • Date: Tue, 5 Aug 2003 09:12:58 -0400

Ok, here's my situation...I've got a build file as follows:

<project>
<property name="predefined" value=""/>
</project>

I'm creating a new property programatically and I want to insert it after
the existing property.
What I do is:
1) Get a list of all properties using the getChildElements() method
2) Get name of last property
3) Get all children of project
4) Loop through children to find an Element with the same name as my
property, assuming unique names
5) Using the loop counter + 1, I get the position in which I want to insert
the new property, in this case (0+1) = 1
6) Call insertChild(Node, 1)

I get the following:

<project>
<property name="newProp1" value=""/>
<property name="predefined" value=""/>
</project>

Any subsequent call to my program will have the same behaviour and insert
the new property after the last inserted and before the predefined one.
So if newProp1 is in index 0 and predefined is in index 1, my new call to
would be insertChild(Node, 2) right?
But it gives me

<project>
<property name="newProp1" value=""/>
<property name="newProp2" value=""/>
<property name="predefined" value=""/>
</project>

Here is a code sample of the method I use:

// Creates an application Element and puts it into the XOM
protected static void createProperty(Document doc, String prop, String
propVal) {
try {
propVal = resolveQuotes(propVal);
Element project = doc.getRootElement();
Element property = new Element("property");
Attribute name = new Attribute("name", prop);
Attribute value = new Attribute("value", propVal);
property.addAttribute(name);
property.addAttribute(value);

/* Figuring out where to insert new property
* First, get the name of the last property
* Second, find the position of that in the children hierarchy
*/
//First
Elements properties = project.getChildElements("property");
Element lastProperty = properties.get(properties.size() - 1);
Attribute attName = lastProperty.getAttribute("name");
String propName = attName.getValue();
//Second
Elements children = project.getChildElements();
int insertPosition = - 1;
for (int i = 0; i < children.size(); i++) {
Element tmp = children.get(i);
Attribute attTmpName = tmp.getAttribute("name");
if (attTmpName != null) {
String tmpName = attTmpName.getValue();
if (tmpName.equals(propName)) {
insertPosition = i + 1;
break;
}
}
}

project.insertChild(property, insertPosition);
BT.isSaved = false;
}
catch (Exception e) {
log.write(ERROR_MSG + "Caught Exception in
Utilities.createProperty: " + e);
}
}

Haig Ehramdjian






  • [XOM-interest] Element.insertChild(Node, int), Haig Ehramdjian, 08/05/2003

Archive powered by MHonArc 2.6.24.

Top of Page