[XOM-interest] Problem to add a new Node

Mikael Petterson mikael.petterson at ericsson.com
Fri Mar 13 11:22:19 EDT 2009


Hi,

Thanks for an excellent piece of software. Easy to use. ( I am using xom
1.1).

I need to add a node to an existing node in an xml document.
So I parse the file and then try to find the node that I wan't to add
another node to.
I find it but It seems like I can only get a list of my children for
that node that I pointed out in my Xpath expression.
Is it not possible to get the node that I searched for so I would have
something like:

Node matchingNode =
doc.query("//con:testCase[@id='2ad7f0bc-efa0-4e79-8068-eee7a0fed9a1']",c
ontext);
Then I gould add to this node and serialize down to file to create the
new resulting xml file.
I might have missed some concepts.

I will attach the xml file I am working on.
 <<defaultdata-soapui-project.xml>> 
All hints and pointers are greatly appreciated.

Br,

//mike

Here is my simple code:
=================
import java.io.File;
import java.io.IOException;


import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Nodes;
import nu.xom.ParsingException;
import nu.xom.ValidityException;
import nu.xom.XPathContext;


public class XomHandler {

	/**
	 * @param args
	 * @throws ParsingException 
	 * @throws ValidityException 
	 */
	public static void main(String[] args) throws ValidityException,
ParsingException {
		
		
		//Read the xml-file
		Builder builder = new Builder();
        Document doc = null;
		try {
			doc = builder.build(new
File("C:\\tmp\\defaultdata-soapui-project.xml"));
		} catch (ValidityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParsingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //System.out.println(doc.toXML());
	
        
        //Now get the node in question.
        
        Element root = doc.getRootElement();
        XPathContext context = new XPathContext("con",
"http://eviware.com/soapui/config");
        //Get the 
        Nodes nodes =
doc.query("//con:testCase[@id='2ad7f0bc-efa0-4e79-8068-eee7a0fed9a1']",c
ontext);
        
        for (int i = 0; i < nodes.size(); i++) {
			System.out.print(nodes.get(i).getValue());
			
		}
        
        //Here I wan't to use the node that I searched for and add some
nodes below it.
        
        
        
        
        
        
		
	}
	


}


=============================
Ericsson AB

Mikael Petterson
Software Designer 
Mobile: +46-(0)70-2673044 
Email: mikael.petterson at ericsson.com
============================= 




More information about the XOM-interest mailing list