I am working on a custom portal to VMware vCenter through the VMware Orchestrator API. I am using savon to query the SOAP API (WSDL) of Orchestrator and have that returning valid data... specifically an XML containing all the Virtual Machines.
What is the best/easiest way to capture the response into a Rails Model? The XML structure of the response is below...
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<findResponse xmlns="http://webservice.vso.dunes.ch">
<findReturn>
<totalCount>4</totalCount>
<elements>
<item>
<type>VC:VirtualMachine</type>
<id>vc.demo.local/vm-37</id>
<properties>
<item>
<name>displayName</name>
<value>FreeNAS</value>
</item>
<item>
<name>isTemplate</name>
<value>false</value>
</item>
<item>
<name>name</name>
<value>FreeNAS</value>
</item>
<item>
<name>connectionState</name>
<value>connected</value>
</item>
<item>
<name>state</name>
<value>poweredOff</value>
</item>
<item>
<name>vimHost</name>
<value>https://vc.demo.local:443/sdk</value>
</item>
<item>
<name>id</name>
<value>vm-37</value>
</item>
<item>
<name>dunesId</name>
<value>vc.demo.local/vm-37</value>
</item>
</properties>
<dunesUri>dunes://service.dunes.ch/CustomSDKObject?id='vc.demo.local/vm-37'&dunesName='VC:VirtualMachine'</dunesUri>
</item>
<item>
...
</item>
</elements>
</findReturn>
</findResponse>
</soapenv:Body>
</soapenv:Envelope>
You could pick out the few key attributes that you may want to query for (name, id, type) and then just store the rest of the XML as a clob or varchar or as a native XML type if your db supports it. Them you can read other values out in code when you need to.
I use the nokogiri gem to parse xml. With nokogiri you can call an xpath to get all elements with a certain parent tag. For example using your xml:
your_Model = Your_Model.new
docs = Nokogiri::XML(your.xml)
your_Model = docs.xpath('//item').map do |i|
{
#here you would need to map the objects of the xml to your model attributes so if your attributes were called, itemname, and itemvalue the code would be.
itemname => i.xpath('name').inner_text,
itemvalue => i.xpath('value').inner_text
}
your_Model.save!
Make sure you require the Nokogiri gem if you decide to go this route.
Hope it helps
Related
Hi I need to convert the following input xml to the below mentioned output xml using xsl 2.0 version. I'm pretty new to xslt and I've tried using apply-templates and for-each-group to get a distinct collection of clients.
Input:
<?xml version="1.0" encoding="utf-8" ?>
<ITEM>
<allCounselling>
<ITEM>
<allAttendingPeople>
<ITEM>
<PersonKey>1</PersonKey>
</ITEM>
</allAttendingPeople>
<allSessions>
<ITEM>
<KEY></KEY>
<DATE>12 Dec 2014</DATE>
<allAttendedPeople>
<ITEM>
<PersonKey>1</PersonKey>
</ITEM>
<ITEM>
<PersonKey>2</PersonKey>
</ITEM>
</allAttendedPeople>
</ITEM>
</allSessions>
</ITEM>
</allCounselling>
</ITEM>
Required Output is
<Clients>
<Client>
<ClientId>1</ClientId>
</Client>
<Client>
<ClientId>2</ClientId>
</Client>
</Clients>
The PersonKey value is the ClientId and should be a distinct collection.
The other difficult part is that I have to filter the sessions also. If the session Item is within a given date range, then I should only out put the persons within that allAttendedPeople collection of those filtered sessions and then I have to traverse up to get the counselling item which contains those sessions and out put all attending people...I know it's pretty hectic :'(
Please help!
I have the xml structure xml like that:
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">
<channel>
<item>
<comments>http://www.rtl.fr/actu/politique/laurent-gerra-imitant-nicolas-sarkozy-il-faut-barrer-la-
<slash:comments>0</slash:comments>
</item>
</channel>
</rss>
How can I get text in <slash:comments> tag.
I already try(Swift version):
elementItem.child("comments",inNamespace: "slash").
But it don't work.
As stated here, RaptureXML deletes namespaces, so you can access your content via
elementItem.child("comments")
I have a small xml document from which I need to extract some values using xmllint. I am able to navigate through the xml hierarchy using xmllint --shell xmlfilename command.
But I am unable to extract the values. I don't want to use a grep / any pattern matching command, as that is already done and is a success.
I would appreciate any help regarding the xmlliint.
Here is my document in png format. I want to extract the 300$ and 500$ (the value).
<?xml version="1`.`0" encoding="ISO-8859-1"?>
<adi>
<asset>
<electronics item="Mobile" name="Nokia" value="300$" />
<electronics item="Mobile" name="Sony" value="500$" />
</asset>
</adi>
Another doubt is, are the two sets, the different representation of same xml ?
<?xml version="1.0 encoding="ISO-8859-1"?>
<adi>
<asset>
<electronics>
<item> Mobile </item>
<name>Nokia</name>
<value>300$</value>
</electronics>
<electronics>
<item> Mobile </item>
<name>Sony</name>
<value>500$</value>
</electronics>
</asset>
</adi>
With regards to your second question, those two snippets do not represent the same XML content. Attributes and child elements are not equivalent. A child element can be the root element of some arbitrary XML tree, but attributes are atomic.
E.g., I could modify the second snippet like this:
<?xml version="1.0 encoding="ISO-8859-1"?>
<adi>
<asset>
<electronics>
<item>
Mobile
<sub-item>Phone</sub-item>
</item>
<name>Nokia</name>
<value>300$</value>
</electronics>
<electronics>
<item> Mobile </item>
<name>Sony</name>
<value>500$</value>
</electronics>
</asset>
</adi>
where I have added <sub-item>Phone</sub-item> to the first <item> element.
However, there's no equivalent if item is an attribute instead, as in the first snippet.
Late but while searches for the tag xmllint match the first page, I answer you now ;)
use --xpath instead of --xpath like below
xmllint --xpath '//electronics/value/text()' second-xml_file.xml
How to do String replace method for a variable wish storing the data with including the xml tags. With out storing in a variable the string replacing method is working but my result is stored in a variable. Every time its keep on updated. That's why the string replacing method must be applied for that variable.
Sample strings:
<item>
<Matkl>001</Matkl>
<Text>Metal processing</Text>
</item>
<item>
<Matkl>00107</Matkl>
<Text>Miscelleanous</Text>
</item>
<item>
<Matkl>002</Matkl>
<Text>Electronics</Text>
</item>
<item>
<Matkl>00207</Matkl>
<Text>Monitors</Text>
</item>
<item>
<Matkl>007</Matkl>
<Text>Services</Text>
</item>
<item>
<Matkl>008</Matkl>
<Text>Packaging</Text>
</item>
<item>
<Matkl>01</Matkl>
<Text>Material Group 01</Text>
</item>
<item>
<Matkl>00208</Matkl>
<Text/>
</item>
<item>
<Matkl>014</Matkl>
<Text/>
</item>
The right thing to do would be to use an NSXMLDocument to parse the string and get the text out, which you then can concatenate to a new string if you wish. Have a look here:
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLDocument_Class/Reference/Reference.html
Alternatively you could use an NSScanner to scan to the first occurrence of '>', then get all text until the occurrence of '<', then scan to the next occurrence of '>', and so on - you get the picture. NSScanner is described here:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/Scanners.html
I am trying to build a Standalone VoiceXML parser which accepts the input as the example below
<grammar version="1.0" root="ROOT" mode="dtmf">
<rule id="ROOT">
<one-of>
<item> 1 </item>
<item> 2 </item>
<item> 3 </item>
<item> 4 </item>
<item> 5 </item>
</one-of>
</rule>
</grammar>
It is easy to write a specific parser for that specific grammar specification. However, I want to have a generic solution/algorithm which can parse/interpret all possible DTMF grammar cases.
I appreciate any help.
Thanks,
-Toan.
There is a very good open source which implements a VXML interpreter. It is JVoiceXML at http://jvoicexml.sourceforge.net/
It also includes a VXML grammar validation engine. I think this is what I am looking for.
Take a look at NuGram.
Open VXI is another open source solution that has been around for a long time. A lot of major telephony vendors have used this as the basis for their IVR platforms.