xom-interest AT lists.ibiblio.org
Subject: XOM API for Processing XML with Java
List archive
- From: Peter Stibrany <pstibrany AT gmail.com>
- To: Mikael Petterson <mikael.petterson AT ericsson.com>
- Cc: xom-interest AT lists.ibiblio.org
- Subject: Re: [XOM-interest] Problem to add a new Node
- Date: Tue, 31 Mar 2009 16:21:42 +0200
Hello,
you can simply take first Element out of returned nodes (make sure
there is one), and add your nodes to this first node.
I.e.
Nodes nodes =
doc.query("//con:testCase[@id='2ad7f0bc-efa0-4e79-8068-eee7a0fed9a1']",context);
if (nodes.size() != 1) {
throw new IllegalStateException("Expected only one node for xpath...");
}
if (!nodes.get(0) instanceof Element) {
throw new IllegalStateException("Expected element to be returned
by xpath...");
}
Element node = (Element) nodes.get(0);
node.appendChild(... your node ...);
Regards,
-Peter Stibrany
2009/3/13 Mikael Petterson <mikael.petterson AT ericsson.com>:
> 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
> =============================
>
>
>
> _______________________________________________
> XOM-interest mailing list
> XOM-interest AT lists.ibiblio.org
> http://lists.ibiblio.org/mailman/listinfo/xom-interest
>
>
-
[XOM-interest] Problem to add a new Node,
Mikael Petterson, 03/13/2009
- Re: [XOM-interest] Problem to add a new Node, Peter Stibrany, 03/31/2009
Archive powered by MHonArc 2.6.24.