This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
So I do various operations to add some xml to a web service, and I get a response like so:
I need to pull out the text node there in the Id tag: 2 from <Id>2</Id> That response is the result of adding a new channel to the web service. Once added, I get a response that looks like the xml above, but I neeed to pull out that text node from the ID tag, and Im not sure how to do so. I was thinking of maybe creating a class that had Status, Code, Detail, Resources, Resource, and ID variables, and then just writing that response to the class, and then do the getMethod from that class to get the ID. Does this sound plausible or is there an easier way.
So I have code that writes the new new channel into an outStream, changes it into various forms like ByteArray and then RequestEntity, etc, and sends that to the web service as xml. Then I get my response which I show above there, but I dont know how to get the Id out of that response.
True wisdom is in knowing you know nothing - Socrates
well that was for parsing xml that comes from the web service that I have classes with variables set up for, and then I take the xml response and populate the class with it. Thats why I was thinking that is how I should do this, just make a class and populate its variables with that response feed, but Im not sure it will work like I think it might...
Here is an example of the xml your talking about:
thats xml that I can either read or write already, because I have a measurementProject class that I morph the xml into
but I have no way to manipulate the response body I get from
At least not yet. I was hoping there would be an easy command I could use to just pull that ID out without having to come up with clsases to hold the variables and such. I need to look into that actually.
========================BELOW THIS IS A VERY DETAILED EXPLANATION SO DONT FEEL OBLIGATED TO READ IT IF YOU DONT WANT TO==================================================
edit - here is a bigger view of the problem - I have a jsf page where a user adds a project and a device belonging to that project to the web service. I have the methods that write the project to the project collection, and I have the method to write the device to the device collection.
but the deviceID also needs to be added to the project metadata like this:
The problem is, the deviceID is automatically assigned by the client upon the device's creation. So upon hitting the submit button, I need to first add the device to the device collection, then pull the newly generated deviceID from that device (the ID the client automatically assigns) and then use that deviceID in the creation of the newproject, so it shows that the dvice belongs to the project.
Then the user hits submit, and the first thing is that the device is added to the deviceCollection:
Then behind the scenes I need to take that data and get the deviceID (in this case device), and use that, along with the project name the user inputs to create a new project and add it to the project collection:
notice how this new project has a space for deviceId, and it is the ID of the device created at the same time as the project.
This all has to be done through one submit button lol.
Im very close already, I mean I can add a project and device at the same time, to their respective collections, but the project does not have the deviceId there in its metadata, rather the above project ends up looking like:
parse which xml, the response body or the other stuff I gave up there? This is the response body:
because if that is what you mean I should parse then that is the option I was asking about in the very first post, if that sounded like a good idea or if there was an easier way? If you mean I should parse any of the other stuff like the device, that wont work. I can do that already, but to do so I have to have the devceID, and with what I am doing here the client is automatically creating the id, so I need the code that goes and gets that ID, then I parse it into a class and then use that ID where needed. And at this point in the process, the only place the ID exists is in that response Body from above there
I have been making SimpleXML work just fine for what Im doing so Im sticking with that, changing now would be to have to change all my work (and there is a lot of it). And I only have till friday to do this. I ended up with a teammate on this who was useless, I gave him one responsibility figuring he would finish it and be able to help me in other areas but for four months now he has made ZERO progress, so now it all falls to me to get things ready by friday!!
@Matt: I'm not really sure what you're saying or asking at this point; if you can already parse XML (I had assumed when I said "dom4j etc." it was obvious *which* method of parsing wasn't relevant) I don't know why you wouldn't just parse this new chunk, and I don't understand why it "won't work".
I guess all I am really asking is if it is even possible to parse a response like this:
If I just make a class with the @Element tags and such and try to pass that response to it will it work? Im pretty sure it will but I just wanted to ask first. As it turns out, I have about 3 other major issues I have to to tackle first anyhow, it sucks when you have useless team members who couldnt even do a a System.out.println() line...
BTW, here is the code that generates that response:
SO I could just take something like method.getResponseBodyAsStream and pass it to my parser to parse into the class i was talking about creating right? SImpleXML isnt my strong point in this project, thats why I want to be sure before I spend the time on trying it
To get the value between <Id></Id>, a script like the following perhaps ?
This script is in biterscripting language, but any scripting language will do. The first stex command (string extractor) extract everything upto <Id> from $xml and sends it to null (thus removes it). The second stex command removes everything starting with </Id>. Whatever is left in $xml is the value, which is echo'ed.
I am using -c (ignore case), so id, Id, ID will all match.
And also a Resource Class that is needed to fill in the Resources List from the class above:
Then I just take the response I get from httpClient when I add a new entity, send it to a SimpleXml method I created, and use that SimpleXml method to fill the Status class with variables. Then i just extract the ID variable and get what I need: