private static void writePairs( Document dom, MVD mvd, 
		Element parent, String encoding ) throws Exception
	{
		Element pParent = dom.createElement( "pairs" );
		HashMap<Pair,Integer> parents = new HashMap<Pair,Integer>();
		HashMap<Pair,LinkedList<Element>> orphans = 
			new HashMap<Pair,LinkedList<Element>>();
		int id = 1;
		for ( int i=0;i<mvd.pairs.size();i++ )
		{
			Pair p = mvd.pairs.get( i );
			Element pElement = dom.createElement( "pair" );
			if ( p.children != null )
			{
				pElement.setAttribute("id", Integer.toString(id) );
				parents.put( p, new Integer(id++) );
				// check if any orphans belong to this parent
				LinkedList<Element> llp = orphans.get( p );
				if ( llp != null )
				{
					Integer pId = parents.get( p );
					for ( int j=0;j<llp.size();j++ )
					{
						Element child = llp.get( j );
						child.setAttribute( "parent", pId.toString() );
					}
					orphans.remove( p );
				}
			}
			else if ( p.parent != null )
			{
				Integer pId = parents.get( p.parent );
				if ( pId != null )
					pElement.setAttribute( "parent", pId.toString() );
				else
				{
					LinkedList<Element> children = orphans.get( p.parent );
					if ( children == null )
					{
						children = new LinkedList<Element>();
						children.add( pElement );
						orphans.put( p.parent, children );
					}
					else
						children.add( pElement );
				}
			}
			if ( p.versions.nextSetBit(0)==0 )
				pElement.setAttribute("hint", 
					Boolean.toString(true) );
			pElement.setAttribute( "versions", 
				serialiseVersion(p.versions) );
			if ( p.parent == null )
			{
				String contents = new String(p.getData(),encoding);
				pElement.setTextContent( contents );
			}
			pParent.appendChild( pElement );
		}
		// attach pairs node to its parent node
		parent.appendChild( pParent );
	}