Getting the XML ID using REXML - ruby-on-rails

I have an XML response as included below. How can I obtain the element?
In rails I have:
xml = REXML::Document.new(contacts_as_xml)
Rails.logger.info xml[1].attributes["id"]
Which is not returning 'user#company.com'
Thanks
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gContact="http://schemas.google.com/contact/2008" xmlns:batch="http://schemas.google.com/gdata/batch" xmlns:gd="http://schemas.google.com/g/2005" gd:etag="W/"Ck8BQ38yfyt7I2A9WhNSFk8."">
<id>user#company.com</id>
<updated>2012-10-30T18:14:12.197Z</updated>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" />
<title>Users's Contacts</title>
<link rel="alternate" type="text/html" href="http://www.google.com/" />
<link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full" />
<link rel="http://schemas.google.com/g/2005#post" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full" />
<link rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full/batch" />
<link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full?max-results=10000" />
<author>
<name>User Howdy</name>
<email>user#company.com</email>
</author>
<generator version="1.0" uri="http://www.google.com/m8/feeds">Contacts</generator>
<openSearch:totalResults>2</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>10000</openSearch:itemsPerPage>
<entry gd:etag=""QXc5eDVSLit7I2A9WhdSFk8PTgU."">
<id>http://www.google.com/m8/feeds/contacts/myuser%40mysite.com/base/970a99881a51f3</id>
<updated>2011-07-25T20:31:50.920Z</updated>
<app:edited xmlns:app="http://www.w3.org/2007/app">2011-07-25T20:31:50.920Z</app:edited>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" />
<title>Jon Paris</title>
<link rel="http://schemas.google.com/contacts/2008/rel#photo" type="image/*" href="https://www.google.com/m8/feeds/photos/media/myuser%40mysite.com/970a99881a51f3" />
<link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full/970a99881a51f3" />
<link rel="edit" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full/970a99881a51f3" />
<gd:name>
<gd:fullName>Jon Adams</gd:fullName>
<gd:givenName>Jon</gd:givenName>
<gd:familyName>Adams</gd:familyName>
</gd:name>
<gd:email rel="http://schemas.google.com/g/2005#other" address="jon#adams.com" primary="true" />
<gd:email rel="http://schemas.google.com/g/2005#home" address="jon#adams.com" />
</entry>
<entry gd:etag=""R3YzcTFbKit7I2A9WhJbE00ORQY."">
<id>http://www.google.com/m8/feeds/contacts/myuser%40mysite.com/base/1229b5e8eeef3e3</id>
<updated>2012-09-22T08:57:06.889Z</updated>
<app:edited xmlns:app="http://www.w3.org/2007/app">2012-09-22T08:57:06.889Z</app:edited>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" />
<title></title>
<link rel="http://schemas.google.com/contacts/2008/rel#photo" type="image/*" href="https://www.google.com/m8/feeds/photos/media/myuser%40mysite.com/1229b5e8eeef3e3" gd:etag=""UQZOJlcjWit7I2BmGBdVTzBfL2E2GGI5Myk."" />
<link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full/1229b5e8eeef3e3" />
<link rel="edit" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full/1229b5e8eeef3e3" />
<gd:email rel="http://schemas.google.com/g/2005#other" address="person#site.com" primary="true" />
<gd:email rel="http://schemas.google.com/g/2005#home" address="person#gmail.com" />
</entry>
</feed>

Problem
When you do:
xml[1].attributes["id"]
You are getting the text node of <?xml version='1.0' encoding='UTF-8'?> and then trying to get its id attribute. Since it does not exist, you got blank.
Solution
You want:
xml.root.elements['id'].text
#=> user#company.com
Note that "user#company.com" is a text node of the id element. It is not an attribute.

Related

TFS OData workitem get last insert id

I'm using TFS OData service to create work item which is work fine. now i need to get id of work item and insert into database for later reference can anyone guide me direction for this ?
here is what i get as a response.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xml:base="https://tfsodata.visualstudio.com/DefaultCollection/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:etag="W/"datetime'2013-08-31T10%3A23%3A21.1439028%2B00%3A00'"" xmlns="http://www.w3.org/2005/Atom">
<id>https://tfsodata.visualstudio.com/DefaultCollection/WorkItems(0)</id>
<title type="text">my test work item</title>
<summary type="text">test work item created by isuru http://www.google.com</summary>
<updated>2013-08-31T10:23:21Z</updated>
<author>
<name />
</author>
<link rel="edit" title="WorkItem" href="WorkItems(0)" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Attachments" type="application/atom+xml;type=feed" title="Attachments" href="WorkItems(0)/Attachments" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Links" type="application/atom+xml;type=feed" title="Links" href="WorkItems(0)/Links" />
<category term="Microsoft.Samples.DPE.ODataTFS.Model.Entities.WorkItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm.Int32">0</d:Id>
<d:Project>My Website</d:Project>
<d:Type>Product Backlog Item</d:Type>
<d:WebEditorUrl m:null="true" />
<d:AreaPath m:null="true" />
<d:IterationPath m:null="true" />
<d:Revision m:type="Edm.Int32">0</d:Revision>
<d:Priority m:null="true" />
<d:Severity m:null="true" />
<d:StackRank m:type="Edm.Double">0</d:StackRank>
<d:AssignedTo m:null="true" />
<d:CreatedDate m:type="Edm.DateTime">2013-08-31T10:23:21.1419032+00:00</d:CreatedDate>
<d:CreatedBy m:null="true" />
<d:ChangedDate m:type="Edm.DateTime">2013-08-31T10:23:21.1439028+00:00</d:ChangedDate>
<d:ChangedBy m:null="true" />
<d:ResolvedBy m:null="true" />
<d:Title>my test work item</d:Title>
<d:State m:null="true" />
<d:Reason m:null="true" />
<d:CompletedWork m:type="Edm.Double">0</d:CompletedWork>
<d:RemainingWork m:type="Edm.Double">0</d:RemainingWork>
<d:Description>test work item created by isuru http://www.google.com</d:Description>
<d:ReproSteps m:null="true" />
<d:FoundInBuild m:null="true" />
<d:IntegratedInBuild m:null="true" />
<d:AttachedFileCount m:type="Edm.Int32">0</d:AttachedFileCount>
<d:HyperLinkCount m:type="Edm.Int32">0</d:HyperLinkCount>
<d:RelatedLinkCount m:type="Edm.Int32">0</d:RelatedLinkCount>
<d:Risk m:null="true" />
<d:StoryPoints m:type="Edm.Double">0</d:StoryPoints>
<d:OriginalEstimate m:type="Edm.Double">0</d:OriginalEstimate>
<d:BacklogPriority m:type="Edm.Double">0</d:BacklogPriority>
<d:BusinessValue m:type="Edm.Int32">0</d:BusinessValue>
<d:Effort m:type="Edm.Double">0</d:Effort>
<d:Blocked m:null="true" />
<d:Size m:type="Edm.Double">0</d:Size>
</m:properties>
</content>
</entry>
What I did was adding a Guid to a random field that you are not using and query for that Guid afterwards.
wi.Title = "New Work Item";
wi.FoundInBuild = Guid.NewGuid().ToString();
wi.Type = "Bug";
wi.Project = "TestProject";
context.AddToWorkItems(wi);
context.SaveChanges();
Debug.WriteLine(String.Format("Work item created"));
var createdWi = context.WorkItems.Where( d => d.FoundInBuild == wi.FoundInBuild);

Spring MVC resource Configuration issue

I have been using spring MVC for while and so far so good. I decided to add jquery UI to my project because I wanted to use jqgrid for my grids. below is the folder structure for the webapp:
-assets
+javascript
+images
-css
+jqgrid
+bootstrap
-jquery
-ui-smoothness
+images
jquery-ui-1.10.3.custom.min.css /** this file is referecing images in the image folder above with the css url() method **/
jquery_{versions}.js
-WEB-INF
+views
spring_application_Contexts.xml
web.xml
In my spring_application_context for mvc i have :
<mvc:resources mapping="/images/**" location="/assets/images/" />
<mvc:resources mapping="/styles/**" location="/assets/css/" />
<mvc:resources mapping="/script/**" location="/assets/js/" />
I can access my files in jsp using this :
<title><spring:message code="dataview.page.title" /> </title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<link href="<spring:url value="/styles/bootstrap/bootstrap.css"/>/" rel="stylesheet">
<link href="<spring:url value="/styles/bootstrap/bootstrap-responsive.css"/>/" rel="stylesheet">
<link href="<spring:url value="/styles/jquery/ui-smoothness/jquery-ui-1.10.3.custom.min.css"/>/" rel="stylesheet">
<link href="<spring:url value="/styles/jqgrid/ui.jqgrid.css"/>/" rel="stylesheet">
<script type="text/javascript" src="<spring:url value="/script/jquery/jquery-1.9.1.min.js"/>"></script>
<script type="text/javascript" src="<spring:url value="/script/jqgrid/jquery.jqGrid.min.js"/>"></script>
<script type="text/javascript" src="<spring:url value="/script/jqgrid/grid.locale-en.js"/>"></script>
<script type="text/javascript" src="<spring:url value="/script/bootstrap/bootstrap.js"/>"></script>
but it complains about files not found like below:
Images url in firebug are of types:
http://localhost:8080/edowmis/styles/jquery/ui-smoothness/jquery-ui-1.10.3.custom.min.css/images/ui-bg_flat_75_ffffff_40x100.png
the images url contains the jquery ui css path.
where is the catch? Kindly help me address this it's something iv'e tried to figure out but in vain
Thanks
EDIT: adding web.xml and some important application context files
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>edowmis</display-name>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>edowmis</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/edowmis-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>edowmisDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/edowmis-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>edowmisDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--Shiro -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Shiro End -->
</web-app>
snippet of main applicationcontext file edowmis-config.xml
<context:component-scan base-package="net.publichealth.edowmis.web.forms"/>
<context:component-scan base-package="net.publichealth.edowmis.web.security"/>
<context:component-scan base-package="net.publichealth.edowmis.web.util"/>
<context:component-scan base-package="net.publichealth.edowmis.web.service"/>
<import resource="classpath:META-INF/edowmis-mongo-config.xml" />
<import resource="edowmis-security-config.xml" />
<import resource="edowmis-config-mvc.xml" />
bellow is main part of mvc context:edowmis-config-mvc.xml
<mvc:annotation-driven/>
<context:annotation-config/>
<context:component-scan base-package="net.publichealth.edowmis.web.controllers"/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/mvc/views/" />
<property name="suffix" value=".jsp"/>
</bean>
<mvc:resources mapping="/images/**" location="/assets/images/" />
<mvc:resources mapping="/styles/**" location="/assets/css/" />
<mvc:resources mapping="/script/**" location="/assets/js/" />
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<bean id="messageConverters"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<!-- Message converters -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>
EDIT 2 head section of the html
<head>
<title>Edowmis:: View Data in a Map </title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<link href="/edowmis/styles/bootstrap/bootstrap.css/" rel="stylesheet">
<link href="/edowmis/styles/jquery/ui-smoothness/jquery-ui-1.10.3.custom.min.css/" rel="stylesheet">
<link href="/edowmis/styles/jqgrid/ui.jqgrid.css/" rel="stylesheet">
<script type="text/javascript" src="/edowmis/script/jquery/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="/edowmis/script/bootstrap/bootstrap.js"></script>
<script type="text/javascript" src="/edowmis/script/jqgrid/jquery.jqGrid.min.js"></script>
<script type="text/javascript" src="/edowmis/script/jqgrid/grid.locale-en.js"></script>
<script type="text/javascript" src="/edowmis/script/jqgrid/jquery.contextmenu.js"> </script>

How to retrieve data using xml

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<MasterDetailsResponse xmlns="http://192.168.100.173/ArvindMill/">
<MasterDetailsResult>
<GetAllMasterDetail>
<TABLENAME>item_master</TABLENAME>
<ITEMID>1</ITEMID>
<ITEMTYPE>CTS</ITEMTYPE>
<GROUP />
<VARIETY />
<FORM />
<STATUS />
<ITEM />
<GRADE />
<TYPE />
</GetAllMasterDetail>
<GetAllMasterDetail>
<TABLENAME>item_master</TABLENAME>
<ITEMID>2</ITEMID>
<ITEMTYPE>AGS</ITEMTYPE>
<GROUP /><VARIETY />
<FORM />
<STATUS />
<ITEM />
<GRADE />
<TYPE />
</GetAllMasterDetail>
<GetAllMasterDetail>
<TABLENAME>tablet_taluka_master</TABLENAME>
<VILLAGE>Anturli</VILLAGE>
<TALUKA>Anturli</TALUKA>
<TABLETUSERCODE />
<TABLETUSERNAME /><TABLETCODE />
<TABLETTALUKAID />
</GetAllMasterDetail>
<GetAllMasterDetail>
<TABLENAME>tablet_taluka_master</TABLENAME>
<VILLAGE>Bortha</VILLAGE>
<TALUKA>Sadgavan</TALUKA>
<TABLETUSERCODE /><TABLETUSERNAME />
<TABLETCODE />
<TABLETTALUKAID /></GetAllMasterDetail>
<GetAllMasterDetail>
<TABLENAME>tablet_taluka_master</TABLENAME>
<VILLAGE>Kukarmunda</VILLAGE>
<TALUKA>Kukarmunda</TALUKA>
<TABLETUSERCODE />
<TABLETUSERNAME /><TABLETCODE /><TABLETTALUKAID />
</GetAllMasterDetail>
The above code is the response returned from soapobject in android. How to retrieve the data from the above XML file? I want to display it in list view and the response returned from the soapobject contains more than one table so how to retrieve it?
If your are using Java use SAX Parser

Conditional linking to an CSS file

In my JSF2 project I want to include following two style sheet to my Facelet page based on a condition which is to be obtained from a managed bean.
<link rel="stylesheet" href="css/menu_style_1.css" type="text/css" />
<link rel="stylesheet" href="css/menu_style_2.css" type="text/css" />
How can I achieve this?
try
<h:outputStylesheet name="css/menu_style_1.css" target="head" rendered="#{myBean.myValue eq 'myCondition1'}"/>
and
<h:outputStylesheet name="css/menu_style_2.css" target="head" rendered="#{myBean.myValue eq 'myCondition2'}"/>
OR... you can do it in one line...
<h:outputStylesheet name="css/#{myBean.myValue == 'true' ? 'menu_style_1.css' : 'menu_style_2.css'}" target="head" />
I guess if you want to stick to the link tag you can try something like that
<link rel="stylesheet" href="css/#{myBean.myValue == 'true' ? 'menu_style_1.css' : 'menu_style_2.css'}" type="text/css" />

grails 2.0 including resources the simple way?

I've been battling various resource inclusion issues in my migration from Grails 1.3.7 from Grails 2.0, probably not understanding a few things to begin with.
Firstly, what does
<g:javascript library="application" />
do? (this was in the default main.gsp provided in Grails 1.3.7).
Secondly, for including jquery across my application, can I just do
<r:require module='jquery' />
<r:layoutResources />
in the top of my main sitemesh page that does the
<g:layoutHead />
...
<g:layoutBody />
and "be done with it", using the
<r:layoutResources />
a second time after the
<g:layoutBody />
Thanks
Yes I struggled a little with this at first too.
So firstly the <g:javascript library="application" /> refers to a module defined in a config/*.Resources.groovy file (default is config/ApplicationResources.groovy), inside that you have named modules, eg:
modules = {
application {
resource url: 'js/jquery/jquery-ui-1.8.15.custom.min.js', disposition: 'head'
}
}
Secondly by example a Grails2 main.gsp (cutdown a lot here):
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><g:layoutTitle default="Grails"/></title>
<link rel="stylesheet" href="${resource(dir: 'css', file: 'main.css')}" type="text/css">
<link rel="stylesheet" href="${resource(dir: 'css', file: 'mobile.css')}" type="text/css">
<link rel="stylesheet" href="${resource(dir: 'css/redmond', file: 'jquery-ui-1.8.15.custom.css')}" type="text/css">
<g:layoutHead/>
<g:javascript library="jquery"/>
<r:require module="application"/>
<r:layoutResources/>
</head>
<body>
<g:layoutBody/>
<r:layoutResources/>
</body>
</html>
Hope that sets you in the right direction

Resources