Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ordering of xml nodes

Moderators: larsivi kris

Posted: 06/27/09 12:04:07

Hi, I want to open and write a xml file using tango.text.xml.Document (tango version 0.99.7). I wonder if it is somehow possible to change the ordering of child elements within one element. Since I need the entries to be order within my prog I surely can do this at runtime by using some attribute for ordering. But having them in correct order within the xml file would be easier and additionally make the file more readable.

Any suggestions would be nice ;-)

Author Message

Posted: 07/03/09 17:30:15

So,

I've solved the Problem ;-) Ok it is not the best programming style but it works:

	void insertBeforeNode(Node node, Node n1){
		Node[] nodes;
		Node cur = node;
		Node parent = node.parent();

		while(cur !is null){
			Node temp = cur;
			cur = cur.nextSibling();
			nodes~=temp;
			temp.detach;
		}
		parent.move(n1);
		foreach(n;nodes){
			parent.move(n);
		}
	}

	void move(doc.Node node, int steps){
		if(steps<0){
			int down=-steps;
			if(node.prevSibling()!is null){
				auto n = node;
				doc.Node n1=n.prevSibling();
				for(int i=0;i<down-1;i++){
					auto n2 = n1.prevSibling();
					if(n2!is null) n1=n2; else break;
				}
					node.detach();
					doc.insertBeforeNode(n1,node);
			}
		}
		if(steps>0){
			int up=steps;
			if(node.nextSibling()!is null){
				auto n = node;
				doc.Node n1=n.nextSibling();
				for(int i=0;i<up-1;i++){
					auto n2 = n1.nextSibling();
					if(n2) n1=n2; else break;
				}
					node.detach();
					if(n1.nextSibling()!is null){
						doc.insertBeforeNode(n1.nextSibling(),node);
					}else n1.parent().move(node);
			}
		}
	}