Advice on a class to represent this xml - java

I want to represent the following xml into a java object, and the ordering matters for the sub-nodes.
<document version="1.1">
<id>abc23423lja123423</id>
<version>
<revision>23</revision>
<date>2011/11/28</date>
</version>
<body>
<title>some title</title>
<desc>some desc</desc>
<full>
<line number="1" count="31" text="some text goes here" />
<line number="2" count="31" text="some text goes here" />
<line number="3" count="31" text="some text goes here" />
</full>
</body>
<author>
<name>John Doe</name>
<address>
<city>mississipi</city>
<country>usa</country>
</address>
<meta-data>
<item key="age">33</item>
<item key="books">19</item>
<item key="related>
<item isbn="2342343242343">some title2</item>
<item isbn="2312888888">other title3</item>
</item>
</meta-data>
</author>
</document>
So my class is Document:
public class Document {
String version;
String id;
}
So the sub-items have to be in the same order, meaning the xml nodes in the meta-data node (item) have to be ordered as they appear in the xml file.
How would you design this class? Using many inner classes or would you do it in a more generic way somehow?

You could have JAXB sort it out for you. Have it generate classes for you and see how you like what it comes up with; tweak as necessary.
I wouldn't have inner classes. I'd create classes like DocumentMetaData; I don't know what your <full> and <item> are about, but those would be List of classes as well.

Related

JSON to XML with type attribute in java

I'm rewriting C# application into java code.
There is REST API which return jsons.
I have to parse json to XML but C# library and Java doing it in difference ways.
How to keep type= attribute in java? I can't use JAXB annotations becouse there are too many objects in response and they might changing. XML.toString(jsonObject) doesn't work for me.
C# parsing is done in this way:
XDocument.load(JsonReaderWriterFactory.CreateJsonReader(Encoding.ASCII.GetBytes(jsonString), new XmlDictionaryReaderQuotas()));
C# result:
<root type="object">
<Items type="array">
<item type="object">
<Name type="string">test</Name>
<Total type="number">12.8000000</Total>
<CurrencyCode type="string">CHF</CurrencyCode>
<Country type="string">CH</Country>
</item>
</Items>
</root>
Java result:
<root>
<Items>
<item>
<Name>test</Name>
<Total>12.8000000</Total>
<CurrencyCode>CHF</CurrencyCode>
<Country>CH</Country>>
</item>
</Items>
</root>
I've used org.w3c.Document and org.w3c.dom.Element and set up attribute "type".
Anyway thanks for help :)

How to get xml attribute value containing multiple colons in a single tag i.e. namespaces?

Here is my XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="XYZ" xmlns:d="http://schemas/services" xmlns:m="http://schemas/metadata" xmlns="http://www.abc.pqr/Atom">
<title type="text">Title</title>
<id>Id</id>
<updated>2018-02-08</updated>
<link rel="self" title="titl" href="title" />
<entry m:etag="W/"25"">
<id>id1</id>
<title type="text">title_text</title>
<updated>2018-02-08</updated>
<author>
<name />
</author>
<link m:etag=""{acx},25"" rel="edit-media" title="title_value" />
<link rel="edit" title="title" />
<link rel="http://az.com/CreatedBy" type="application/atom+xml;type=entry" title="CreatedBy" href="title/CreatedBy" />
<link rel="http://az.com/ModifiedBy" type="application/atom+xml;type=entry" title="ModifiedBy" href="title/ModifiedBy" />
<link rel="http://az.com/CheckedOutTo" type="application/atom+xml;type=entry" title="CheckedOutTo" href="title/CheckedOutTo" />
<category term="xyz" scheme="xyz" />
<content type="application/octetstream" src="http://collb.xlsm" />
<m:properties xmlns:m="http://xyz/metadata" xmlns:d="wsx/dataservices">
<d:ContentTypeID>contentId</d:ContentTypeID>
<d:Dept>21</d:Dept>
<d:Class>0</d:Class>
<d:Id m:type="Edm.Int32">2121</d:Id>
</m:properties>
</entry>
<link rel="next" href="http://tyu.com" />
</feed>
I want to read values corresponding to <d:Id m:type> and <entry m:etag> i.e. output should be 2121 and 25.
How should I read these.
P.S: I have gone through similar questions asked on stackoverflow, but nothing worked for me.
Part of my code:
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilder = builderFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(connection.getInputStream());
Element element = doc.getDocumentElement();
element.getAttributeNodeNS("http://schemas/metadata", "type");
You haven't said which of the vast choice of Java APIs you are using. I guess that probably means you're using DOM. I don't know why anyone still uses DOM when there are much better alternatives available, but they do.
Whichever API you are using, you'll find that there's probably a method something like
element.getAttributeValue(namespace, localname)
To read the m:type attribute, you would use
element.getAttributeValue("http://schemas/metadata", "type")

What is a <selector> element?

I read the android manual to learn more about drawable and this is what I have came across. Stated in the android Developer:
item
Defines a drawable to use during certain states, as described by its attributes. Must be a child of a selector element.
1) I am confused about what a selector element is.
2) why do I have to include my item inside the selector?
Selector is the "container" element.
Take for example this playbutton.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="#drawable/pause" /> <!-- pressed -->
<item android:state_checked="true"
android:drawable="#drawable/play" /> <!-- focused -->
<item android:drawable="#drawable/pause" /> <!-- default -->
</selector>
(stolen from here)
It's a single button image that dynamically shows different images depending on whether it is currently pressed or not. The system can "select" the item from a list of items and so it was named selector, I guess.
Or "state list" like in the documentation: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Search And Replace A Line In Java When The String Have Many Same Other Strings

Ok you might have got idea what i am thinking
okay i will go step by step to make things clear
i have a file in android like here :- /sdcard/file.xml
now i want to search the string name which is also present as same in this file
like i have text like this in xml
this is file.xml on different device
<any name="class">
<index="23" />
<item name="item1" />
<item name="item2" />
<item name="item3" />
</any>
<any name="school">
<index="12" />
<item name="item2" />
<item name="item3" />
<item name="item1" />
</any>
this is file.xml on different device
<any name="class">
<index="26" />
<item name="item2" />
<item name="item1" />
<item name="item3" />
</any>
<any name="school">
<index="11" />
<item name="item1" />
<item name="item3" />
<item name="item2" />
</any>
i am taking
<any name="class">
to search the word as it can not be different on devices
but as you can see here the next index is different and items can be up and down as well
but actually i want to replace item2 where ever it exist in <any name="class">
any way i can replace this ?
You can use regular expressions:
String updatedXML = inputXML.replaceAll("<any name=\"(\\d+)\">",
"<myreplacement id=\"$1\">");
Here (\\d+) means "any number of digits can be here, assign them to the group#1" and $1 means "insert group#1 contents here".

I18n in Struts-menu with Struts 2

I've developed a menu using struts-menu 2.4.3 with Struts 2. It's working ok but I need to take the titles of the menus from a properties file (router-messages.properties). The whole application is internationalized like this. But it just doesn't work with struts-menu. Here is my menu-config.xml file:
<Menu name="mainMenu" title="" >
<Item name="ecfMenu" title="ECFs" roles="FISCO" location="DisconnectedEcfsSearch" />
<Item name="reportsMenu" title="Relatórios" roles="ADMIN" location="AlarmReportSearch"/>
<Item name="fiscoMenu" title="Fisco" roles="ADMIN" location="UpdateFiscoForm"/>
<Item name="alarmMenu" title="Alarmes" roles="ADMIN" location="AlarmNotification"/>
<Item name="userMenu" title="application.header.users" roles="ADMIN" location="UserSearch"/>
<Item name="consoleMenu" title="Console" roles="FISCO" location="ConsoleSearch"/>
</Menu>
Please notice that I'm trying to use a key to my properties file in the item 'userMenu'. All other items work, but not this one.
Here's my jsp:
<menu:useMenuDisplayer permissions="rolesAdapter" name="Velocity"
config="/WEB-INF/tabs.html">
<menu:displayMenu name="mainMenu" />
</menu:useMenuDisplayer>
I searched around and even found a guy with the same question but there was no response for him. =/
Does anybody know how to make struts-menu recognize that I'm using a key to a properties file and not a literal String??
Thanks!
Try this:
For the menu-config.xml:
<Menu name="mainMenu" title="" >
<Item name="userMenu" title="application.header.users" roles="ADMIN" location="UserSearch"/>
</Menu>
For the JSP:
Use this:
<%# taglib prefix="menu" uri="http://struts-menu.sf.net/tag-el"%>
Instead of this:
<%# taglib prefix="menu" uri="http://struts-menu.sf.net/tag"%>
Invoking the menu:
<menu:useMenuDisplayer bundle="messages" permissions="rolesAdapter" name="Velocity"
config="/WEB-INF/tabs.html">
<menu:displayMenu name="mainMenu" />
</menu:useMenuDisplayer>
For the bundle="messages" provide the name of your resource bundle.
I hope it works for you!

Categories