XStream with HashMap<String,String> - java

Anyone could tell me how to Serialize a HashMap with XStream?
private HashMap<String,String> attributes;
attributes = new HashMap<String,String>();
attributes.put("Description","Value");
attributes.put("Description2","Value2");
attributes.put("Description3","Value3");
My xml looks like
<attributes>
<entry>
<string>Description</string>
<string>value</string>
</entry>
<entry>
<string>Description2</string>
<string>Value2</string>
</entry>
<entry>
<string>Description3</string>
<string>Value3</string>
</entry>
</attributes>
I want an output like
<attributes>
<attr>
<description>Description</description>
<value>Value</value>
</attr>
<attr>
<description>Description2</description>
<value>Value2</value>
</attr>
<attr>
<description>Description3</description>
<value>Value</value>
</attr>
</attributes>
how can achieve that using XStream? Is possible with annotations?

If you are using XStream 1.4.5, you have the NamedMapConverter to do what you want.
Just register the converter showing how you want to marshal your map as the example bellow:
XStream xstream = new XStream();
NamedMapConverter namedMapConverter = new NamedMapConverter(xstream.getMapper(),"attr","description",String.class,"value",String.class);
xstream.registerConverter(namedMapConverter);

Related

How to remove namespace from Jaxb2 generated xml

I have a problem a little problem related to the namespaces in the XML marshalled by Jaxb2 . When I print out the xml file, I get this "extra" annotations on the xml tags. Is there any way to get rid of the extra namespace annotations ?
Here is an example:
<?xml version="1.0" encoding="UTF-8" standalone="yes">
<root xmlns:ns2="http://www.something.com/something">
<ns2:food>steak</ns2:food>
<ns2:beverage>water</ns2:beverage>
</root>
I want to get rid of the ns2: namespace.
I tried with :
<bean id="glsJaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="${gls.jaxb2.contextpath}"/>
<property name="marshallerProperties">
<map>
<entry key="com.sun.xml.bind.namespacePrefixMapper">
<bean class="es.fs.fswarehousing.gls.ws.EmptyNamespacePrefixMapper"/>
</entry>
</map>
</property>
</bean>
But im getting :
ERROR [TaskExecutor-master-555-ProcessTask [8796749431734]]
[ActionNode] Error executing
org.springframework.oxm.UncategorizedMappingException: Unknown JAXB
exception; nested exception is javax.xml.bind.PropertyException: name:
com.sun.xml.bind.namespacePrefixMapper value:
es.fs.fswarehousing.gls.ws.EmptyNamespacePrefixMapper#17f6a8fe
Can someone give me any hint :) ?

Reading an Xml file into map

I have an xml string in the following format
<map>
<entry>
<string>key</string>
<string>value</string>
</entry>
<entry>
<string>key2</string>
<string>value2</string>
</entry>
....
</map>
Is there an easy library/tool that I can use to read this type of xml directly into a Hashmap. I am not very familiar with working with xml, so I would really appreciate some help here.
Thanks.

How to define Hashmap in an XSD?

I am trying to make an xsd file out of a json response that I have got and later using that xsd to generate JAXB classes. My problem is that the json is in a format I am unable to convert to xsd. Here is my json :
{
"result1":[{"010201":[3,2,0,4,3,4]},{"010202":[1,6,3,2,2,2]},{"010203":[2,3,3,4,4,0]}],
"result2":[1,6,3,2,2,2],
"result3":[{"010201":[3,2,0,3,4]},{"010202":[1,6,3,2,2]},{"010203":[2,3,4,4,0]}],
"result4":[1,4,6,4,1],
"result5":[{"010201":34},{"010202":12},{"010203":78}],
"result6":41
}
Here is the above json in xml format :
<?xml version="1.0" encoding="UTF-8" ?>
<result1>
<010201>3</010201>
<010201>2</010201>
<010201>0</010201>
<010201>4</010201>
<010201>3</010201>
<010201>4</010201>
</result1>
<result1>
<010202>1</010202>
<010202>6</010202>
<010202>3</010202>
<010202>2</010202>
<010202>2</010202>
<010202>2</010202>
</result1>
<result1>
<010203>2</010203>
<010203>3</010203>
<010203>3</010203>
<010203>4</010203>
<010203>4</010203>
<010203>0</010203>
</result1>
<result2>1</result2>
<result2>6</result2>
<result2>3</result2>
<result2>2</result2>
<result2>2</result2>
<result2>2</result2>
<result3>
<010201>3</010201>
<010201>2</010201>
<010201>0</010201>
<010201>3</010201>
<010201>4</010201>
</result3>
<result3>
<010202>1</010202>
<010202>6</010202>
<010202>3</010202>
<010202>2</010202>
<010202>2</010202>
</result3>
<result3>
<010203>2</010203>
<010203>3</010203>
<010203>4</010203>
<010203>4</010203>
<010203>0</010203>
</result3>
<result4>1</result4>
<result4>4</result4>
<result4>6</result4>
<result4>4</result4>
<result4>1</result4>
<result5>
<010201>34</010201>
</result5>
<result5>
<010202>12</010202>
</result5>
<result5>
<010203>78</010203>
</result5>
<result6>41</result6>
I am facing no issues in creating complex types for result2, result4, result6 elements. But result1, result3 and result5 have an array of array of integers. How do i create an xsd for that ? Can anyone help ?
Consider trying an XSD to generate an XML like the below:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<result1>
<entry>
<key>010201</key>
<value>
<val>3</val>
<val>2</val>
<val>1</val>
</value>
</entry>
<entry>
<key>010201</key>
<value>
<val>3</val>
<val>2</val>
<val>1</val>
</value>
</entry>
</result1>
<result2>
<value>
<val>3</val>
<val>2</val>
<val>1</val>
</value>
</result2>
<result3>
<entry>
<key>010201</key>
<value>
<val>3</val>
<val>2</val>
<val>1</val>
</value>
</entry>
<entry>
<key>010201</key>
<value>
<val>3</val>
<val>2</val>
<val>1</val>
</value>
</entry>
</result3>
<result4>
<value>
<val>3</val>
<val>2</val>
<val>1</val>
</value>
</result4>
<result5>
<entry>
<key>010201</key>
<value>2</value>
</entry>
<entry>
<key>010201</key>
<value>2</value>
</entry>
</result5>
</root>
The xsd will be simpler for the above. Keeping the values as XML tags will not be feasible if the values will change.

schema for hudson job config.xml

am new to HUDSON ci-server . i have a requirement stating that i have to use Hudson remote access API and create new jobs in hudson or edit the existing job's configuration file and update it.
I have a sample config.xml file .
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description>Free style Basic sample Job for POC . will create this job with all the required fields . then will take this config.xml and will try to validate and study this schema .</description>
<project-properties class="java.util.concurrent.ConcurrentHashMap">
<entry>
<string>hudson-plugins-disk_usage-DiskUsageProperty</string>
<base-property>
<originalValue class="hudson.plugins.disk_usage.DiskUsageProperty"/>
<propertyOverridden>false</propertyOverridden>
</base-property>
</entry>
<entry>
<string>logRotator</string>
<log-rotator-property>
<originalValue class="hudson.tasks.LogRotator">
<daysToKeep>5</daysToKeep>
<numToKeep>-1</numToKeep>
<artifactDaysToKeep>-1</artifactDaysToKeep>
<artifactNumToKeep>-1</artifactNumToKeep>
</originalValue>
<propertyOverridden>false</propertyOverridden>
</log-rotator-property>
</entry>
<entry>
<string>scmCheckoutRetryCount</string>
<integer-property>
<originalValue class="int">5</originalValue>
<propertyOverridden>false</propertyOverridden>
</integer-property>
</entry>
<entry>
<string>hudson-tasks-Mailer</string>
<external-property>
<originalValue class="hudson.tasks.Mailer">
<recipients>abc#example.com</recipients>
<dontNotifyEveryUnstableBuild>false</dontNotifyEveryUnstableBuild>
<sendToIndividuals>false</sendToIndividuals>
</originalValue>
<propertyOverridden>false</propertyOverridden>
<modified>true</modified>
</external-property>
</entry>
<entry>
<string>hudson-plugins-build_timeout-BuildTimeoutWrapper</string>
<external-property>
<originalValue class="hudson.plugins.build_timeout.BuildTimeoutWrapper">
<timeoutMinutes>5</timeoutMinutes>
<failBuild>false</failBuild>
</originalValue>
<propertyOverridden>false</propertyOverridden>
<modified>true</modified>
</external-property>
</entry>
<entry>
<string>builders</string>
<describable-list-property>
<originalValue class="hudson.util.DescribableList">
<hudson.tasks.Ant>
<targets></targets>
<antName>org.apache.ant_1.7.1</antName>
</hudson.tasks.Ant>
<hudson.tasks.Shell>
<command>HUDSON_USER</command>
</hudson.tasks.Shell>
</originalValue>
<propertyOverridden>false</propertyOverridden>
</describable-list-property>
</entry>
<entry>
<string>hudson-triggers-SCMTrigger</string>
<trigger-property>
<originalValue class="hudson.triggers.SCMTrigger">
<spec>#daily</spec>
</originalValue>
<propertyOverridden>false</propertyOverridden>
</trigger-property>
</entry>
<entry>
<string>jdk</string>
<string-property>
<originalValue class="string">(Inherit From Job)</originalValue>
<propertyOverridden>false</propertyOverridden>
</string-property>
</entry>
<entry>
<string>hudson-tasks-ArtifactArchiver</string>
<external-property>
<originalValue class="hudson.tasks.ArtifactArchiver">
<compressionType>GZIP</compressionType>
<latestOnly>false</latestOnly>
<autoValidateFileMask>false</autoValidateFileMask>
</originalValue>
<propertyOverridden>false</propertyOverridden>
<modified>true</modified>
</external-property>
</entry>
<entry>
<string>scm</string>
<scm-property>
<originalValue class="hudson.scm.NullSCM"/>
<propertyOverridden>false</propertyOverridden>
</scm-property>
</entry>
<entry>
<string>hudson-plugins-release-ReleaseWrapper</string>
<external-property>
<originalValue class="hudson.plugins.release.ReleaseWrapper">
<releaseVersionTemplate></releaseVersionTemplate>
<doNotKeepLog>false</doNotKeepLog>
<overrideBuildParameters>false</overrideBuildParameters>
<parameterDefinitions/>
<preBuildSteps/>
<postBuildSteps/>
<postSuccessfulBuildSteps/>
<postFailedBuildSteps/>
</originalValue>
<propertyOverridden>false</propertyOverridden>
<modified>true</modified>
</external-property>
</entry>
</project-properties>
<keepDependencies>false</keepDependencies>
<creationTime>1403865219075</creationTime>
<properties/>
<cascadingChildrenNames class="java.util.concurrent.CopyOnWriteArraySet"/>
<cascading-job-properties class="java.util.concurrent.CopyOnWriteArraySet">
<string>hudson-plugins-batch_task-BatchTaskProperty</string>
<string>hudson-plugins-disk_usage-DiskUsageProperty</string>
<string>hudson-plugins-jira-JiraProjectProperty</string>
<string>org-hudsonci-plugins-snapshotmonitor-WatchedDependenciesProperty</string>
<string>hudson-plugins-promoted_builds-JobPropertyImpl</string>
</cascading-job-properties>
<scm class="hudson.scm.NullSCM"/>
<canRoam>false</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<cleanWorkspaceRequired>false</cleanWorkspaceRequired>
</project>
Now i have a tag called project-properties . in it i have multiple entry tags .each enrty tag has some sub tags among which string is common to each entry tag .I want to access all these different tags through java using Hudosn remote access API .
for example :
<entry>
<string>logRotator</string>
<log-rotator-property>
<originalValue class="hudson.tasks.LogRotator">
<daysToKeep>5</daysToKeep>
<numToKeep>-1</numToKeep>
<artifactDaysToKeep>-1</artifactDaysToKeep>
<artifactNumToKeep>-1</artifactNumToKeep>
</originalValue>
<propertyOverridden>false</propertyOverridden>
</log-rotator-property>
</entry>
so i want to acceess daysToKeep numToKeep artifactDaysToKeep and so on for the rest of tags and other tags present in the config.xml
Any help will be appreciated.
Thanks in advance.
You could do this by getting an instance of the Item and then updating the project configuration by using the following code
Source streamSource = new StreamSource(new StringReader(config));
try {
proj.updateByXml(streamSource);
} catch (IOException ex) {
Logger.getLogger(PluginImpl.class.getName()).log(Level.SEVERE, null, ex);
}
the config is the string generated config file.
For more info refer to this post How to update Jenkins config.xml from Java code?

Castor custom collection field handler

I'd like to unmarshal the following xml to a MultiKeyMap:
<map>
<entry key1="foo" key2="foo">
<value property="val"/>
</entry>
<entry key1="bar" key2="bar">
<value property="val"/>
</entry>
</map>
I took a look through the Castor API and saw that there's a CollectionFieldHandler class, but I can't find any documentation on where I would need to register a custom collection handler.
First of all, collection isn't a map.
You can implement and register your own handler.
See http://www.castor.org/xml-fieldhandlers.html

Categories

Resources