How to Parse XML Document By Class Name Using Java - java

I'm writing a parsing tool to compare the textual content of two bean XML files in Java. The text content changes and we need a way to run a script to make sure the textual content is the same. I know we have org.w3c.dom which has a method getElementsByTagName("tag_name") and that returns a node list in the XML document. I'm wondering if anyone knows of a way to do this using the class name? I've been searching around but haven't been able to solve this yet.
<bean class="com.mycompany.myText" id="Q1.4">
<property name="name">
<value>Q1.4</value>
</property>
<property name="text">
<value>This is text one</value>
</property>
<property name="retired">
<value>true</value>
</property>
</bean>
<bean class="com.mycompany.myText" id="Q1.5">
<property name="name">
<value>Q1.5</value>
</property>
<property name="text">
<value>This is text two</value>
</property>
<property name="retired">
<value>true</value>
</property>
</bean>
I can't use the 'bean' element name as there are several other beans whith non relevant stuff I need only the ones with the class com.mycompany.myText and the value I'm trying to extract is property name=text - the text content
Any help here would be appreciated. Also as a side note I should mention that we have no direct control of how the XML file is managed and structured as it's fed to us from a 3rd party.

As you say, you need to select tags by their attributes.
You could use XPath to achieve this. See those resources:
http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/
https://stackoverflow.com/a/24220968/6377268
The expression would be something like this
/bean[#class='com.mycompany.myText']/property[#name='text']/value
I hope I could help at least a little bit.

Related

Using CSVParser to parse a tab-delimited file

I need to parse a file which is tab delimited. I'm attempting to use CSVBeans version 0.7 to do this. In an XML configuration file, I have to pass a separator value to indicate how fields are delimited, as follows:
parser className="org.csvbeans.parsers.CSVParser"/>
</strategy>
<property name="separator" value="\t" />
<property name="noStartTag" value="true" />
<converters>
As shown above, I have tried value="\t" />, but it's not working. I have also tried '\\t' and '\t' but to no avail. What value should I use for a tab character?
folks it would be highly appreciated if you guys please let me know the solution specific to csv beans 0.7.1 jar .
In XML a tab character is represented as . So, your file should be like:
<parser className="org.csvbeans.parsers.CSVParser"/>
</strategy>
<property name="separator" value=" " />
<property name="noStartTag" value="true" />
<converters>

Spring inject values

I am very new to spring and started using.
I have a requirement where i have something like properties
like regions..US,UK
Regions
-------
US
UK
And when i read US it shud have values something like
US
----
(KEY)primary----VALUE(primaryValue)
(KEY)secondary----VALUE(secondaryValue)
.
.
similarly
UK
--
(KEY)primary----VALUE(primaryValue)
(KEY)secondary----VALUE(secondaryValue)
.
.
and the regions might increase as requirement changes and the key value pairs below it too
Someone hint me so i can proceed
Thank you in advance
You need to create two bean a List and a Map, in other word List<Map> is you need
<bean id="regions" class="java.util.ArrayList">
<constructor-arg>
<list>
<ref bean="usMap" />
<ref bean="ukMap" />
</list>
</constructor-arg>
</bean>
and
<util:map id="usMap" map-class="java.util.HashMap">
<entry key="primary" value="someValue"/>
<entry key="secondary" value="someValue"/>
</util:map>
You can make different properties in according to region , when server will start all the properties file will load.You can make PropertiesFileReader.java file which will read u'r properties.

Spring + Tiles how to use EL in tiles.xml

I have tiles set up in my spring project as the view handler like this:
<bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver" />
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/views/**/tiles.xml</value>
</list>
</property>
</bean>
I'd like to use EL to access the session scope inside tiles.xml, for resolving a jsp file name. This should be possible if i use CompleteAutoloadTilesContainerFactory, as described here:
http://tiles.apache.org/framework/tutorial/advanced/el-support.html
How can I set my spring tiles configuration to allow this. Ive tried using EL as it is currently set up but the EL is not parsed.
Figured it out, i left the configuration exactly the same and just included tiles-el.jar . No need to include tiles-extras.jar though. NFV
You may try setting completeAutoload property of TilesConfigurer class. Please see reference doc TilesConfirurer.
This may require Tiles-Extras 2.2 jar file.

EL expressions in Apache tile definition is not processed

I am using Apache tiles for templating and part of the template is a header text. This text depends on the section the page belongs to. Each page contains a bean and the header text is built using the properties of that bean. The bean will have a different name for each page.
So, in my JSP file I would have something like this:
<div>${myBean.id} - ${myBean.name}</div>
I want to get that expression in the tile definition and I tried this:
<definition template="/WEB-INF/tiles/layout/mytemplate.jsp">
<put-attribute name="title" expression="${myBean.id} - ${myBean.name}" />
</definition>
And in the template I do:
<div class="title-header"><tiles:insertAttribute name="title" /></div>
But the result is the unprocessed EL expression:
<div>${myBean.id} - ${myBean.name}</div>
The code has been simplified here to keep this post concise but this is exactly what I'm trying to do. There are also reasons why I am trying to do it this way.
Any idea why the EL expresion is not being processed?
Thanks
NOTE: I am fairly new to JSP and Apache Tiles so I may not have used the correct terminology.
I just wanted to point out that Barry's answer (in his comment on the original post) helped me out. You need to have tiles-el.jar on your classpath (if you want to use the standard EL; presumably you need the corresponding JARs for MVEL or OGNL).
Tiles 2. Regarding AttributeEvaluator, here's how you can set that up if you're using Spring:
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles/**/views.xml</value>
</list>
</property>
<!-- Initialize expression language support for use in Tiles definitions. -->
<property name="tilesProperties">
<props>
<prop key="org.apache.tiles.evaluator.AttributeEvaluator">org.apache.tiles.evaluator.el.ELAttributeEvaluator</prop>
</props>
</property>
</bean>
Tiles 3. Spring's TilesConfigurer for Tiles 3 automatically checks the classpath for the JSP API 2.1 and Tiles EL JARs. If it finds them both, it automatically creates an EL-aware attribute evaluator.

Spring MVC one form feeding data to another form

My starting problem is this error message:
Problem accessing /segment.htm.
Reason:
Neither BindingResult nor plain target object for bean name
'acceptCorrected' available as request
attribute
The top-level description of what I'm working on is this:
Form1 solicits some input from the user. When form1 submits, I need to push that data through some processing, and then present form2, containing the results of the processing. I am trying to communicate the results of form1 to form2 via the model returned by form1's controller's onSubmit.
There's reason to believe that this is verboten.
But, if it is, how do I get the data from1 to be available when rendering the JSP page for form2?
<bean name="/segment.htm" class="com.basistech.rseharvest.SegmentFormController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="segment"/>
<property name="commandClass" value="com.basistech.rseharvest.Segment"/>
<property name="validator">
<bean class="com.basistech.rseharvest.SegmentValidator"/>
</property>
<property name="formView" value="segment"/>
<property name="successView" value="showSegmented"/>
<property name="segmenter" ref="segmenter"/>
</bean>
<!-- the page to enter text -->
<bean name="/showSegmented.htm" class="com.basistech.rseharvest.AcceptCorrectedFormController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="acceptCorrected"/>
<property name="commandClass" value="com.basistech.rseharvest.AcceptCorrected"/>
<property name="validator">
<bean class="com.basistech.rseharvest.CorrectionsValidator"/>
</property>
<property name="formView" value="showSegmented"/>
<property name="successView" value="segment"/>
<property name="data" ref="data"/>
</bean>
The onSubmit of the first form returns a ModelAndView pointing to the second form, by naming the same view as you see as the formView of the second form.
Can you explain a little further what this means? Is the "segment" view rendering HTML that contains a <form> that POSTs to /showSegmented.htm?
formBackingObject() is only called on the initial GET request to the page.
Do you want /segmented.htm to display the form and /showSegmented.htm to handle the processing of the input values?
If so, this isn't really the intended usage of AbstractFormController. This class is intended to be used in situations where you want the same controller to handle 1) the presentation of the form and 2) the processing of it's submission.
Hours of messing around seem to have left me with an answer. I'm not sure it's perfect, but it might help other people. There are tons of people frustrated out there with the error message at the top of this question.
In Spring 2.5.6, there is either a bug or a strange limitation, as follows. If a SimpleFormController specifies, as its successView, another page with a SimpleFormController, the second one will fail as in the question. The post data from the first form seems to interfere with what happens on the second form. There seems to be an assumption that the next view after a form is submitted is always a non-form.
If the first page uses a RedirectView to hand off to the second page, all is well.
There are many conflicting pieces of advice, in some cases coming from conflicting heavy-hitters at on the Spring forums, about this. All I know is I made the error posted in the question go away by switching to a redirect. Now the browser does a plain GET of the page for the successView, and the model sets up correctly, and all is well.
Note in passing that the argument to RedirectView is the 'outside' URL (in the tutorial, something like blah.htm) and not the simple token that the mapper maps to JSP pages.

Categories

Resources