Parsing XML with object reference creation - java

I'm parsing xml file and trying to create model from it. I'm using Simple XML library. My xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<root cycles_count="2">
<shifts>
<shift id="0" name="first"/>
<shift id="1" name="second"/>
<shift id="2" name="third"/>
<shift id="3" name="fourth"/>
</shifts>
<cycles>
<cycle name="A" start_date="1334620800000">
<cycle_shift id="0" />
<cycle_shift id="0" />
<cycle_shift id="1" />
</cycle>
<cycle name="B" start_date="1334620800000">
<cycle_shift id="1" />
<cycle_shift id="1" />
<cycle_shift id="2" />
</cycle>
</cycles>
</root>
Is there any way how to create object reference from cycle_shift to shift based on the same id? I want to achieve something like this (simplified version):
#Root
public class Shift {
#Attribute
int id;
#Attribute
String name;
}
#Root
public class Cycle {
#ElementList
List<Shift> shifts; // shifts connected by id's
}
Change of xml schema is possible too. Thanks in advance.

Cycle through the Shifts and create those objects
Create a dictionary with the id of the Shift as the key and the Shift itself as the value
Loop through all your Cycles and create those objects
When creating the List of Shifts belonging to that Cycle, just reference the id from the Cycle's property and look up the Shift from the dictionary you created earlier and add it to the list.
Would that work?

You can set the name attribute optional on Shift, and get rid of cycle_shift and use only shift instead
<?xml version="1.0" encoding="utf-8"?>
<root cycles_count="2">
<shifts>
<shift id="0" name="first"/>
<shift id="1" name="second"/>
<shift id="2" name="third"/>
<shift id="3" name="fourth"/>
</shifts>
<cycle name="B" start_date="1334620800000">
<cycle_shift id="1" />
<cycle_shift id="1" />
<cycle_shift id="2" />
</cycle>
And your pseudo code will be like this: (I'm using the inline modifier)
#Root
public class Cycle {
#ElementList(inline=true)
List<Shift> shifts; // shifts connected by id's
}

Related

java XML parsing using jaxb or dom?

I have a following xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Abc version="3" xmlns="urn:Abc-v3">
<Ele1>
<SubElement name ="name" description="DO">
<Node version="C" siteURL="https://example.com" />
<Client>
<ClientId>1</ClientId>
<ClientSecret>Yes</ClientSecret>
</Client>
</SubElement>
<SubElement name ="SharePointOLDev1" description="DEV1">
<Node version="C" siteURL="https://example.com" />
<Local>
<LocalId>id</LocalId>
<Password>password</Password>
</Local>
</SubElement>
<SubElement name="AS" description="wow">
<DB connection="connection" />
</SubElement>
</Ele1>
<Ele2>
<Content ID="A" co="LD">
<Description>Simple Docs</Description>
<Serve
Lib1="TEST"
Lib2="yes"
Lib3="yes"
Lib4="no"
Lib5="no"
Lib6="name">
<Hole enabled="false" count="200" />
<Fol enabled="false" count="100" />
<Role enabled="No" validate="false" />
<FetchFilenameAttribute connection="SAP-AS" delay="3" />
</Serve>
</Content>
<Content ID="B" co="OL">
<Description>Simple Docs </Description>
<Serve
Lib1="TEST"
Lib2="yes"
Lib3="yes"
Lib4="no"
Lib5="no"
Lib6="name"">
<Hole enabled="false" count="200" />
<Fol enabled="false" count="100" />
<Role enabled="No" validate="false" />
</Serve>
</Content>
</Ele2>
<Ele3>
<CNode attr="hai" attr1="bye" />
</Ele3>
</Abc>
I need to parse this XML file and assign values to its corresponding class objects.Which is the best option to parse such an xml file.
JAXB sounds good to me but the POJOs were already written by someone and now i will have to rewrite and deploy them.ALso teh xml file has some errors while running xjc command.
DOM approach seems to be very cumbersome n error prone.
Please suggest.
PS:Kindly excuse my beginner level knowledge.
the JDK project comes with SAX(Simple API for XML) accessible by importing org.xml.sax.*.
You may take a look at this https://www.tutorialspoint.com/java_xml/java_sax_parse_document.htm for an introduction to the subject.

How do I databind an object of the String[] type?

I already know that I can set the types for many other lists, but for String[] in particular, I currently am having to set the type as Object and cast it later to String[].
Using String[] is not accepted either.
e.g.
<data>
<variable name="example" type="Object" />
</data>
<Linearlayout>
<android.support.v7.widget.AppCompatSpinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#{(String[]) example}"/>
</LinearLayout>
Looking further into the String[] type, it seems that it isn't a complete type (no size defined) and it doesn't validate any resources. The only valid collection types that the Android docs mention are in the following example:
<data>
<import type="android.util.SparseArray"/>
<import type="java.util.Map"/>
<import type="java.util.List"/>
<variable name="list" type="List<String>"/>
<variable name="sparse" type="SparseArray<String>"/>
<variable name="map" type="Map<String, String>"/>
<variable name="index" type="int"/>
<variable name="key" type="String"/>
</data>
…
android:text="#{list[index]}"
…
android:text="#{sparse[index]}"
…
android:text="#{map[key]}"
Perhaps you could use stringArray:
#{#stringArray/fi}
Instead of
#{(String[])
There are a few examples online which may be helpful: android:entries in recyclerview
Check this from android site :

Parse XML files Java with DOM

I am currently developing a system that uses the following format of XML files to for configuration purposes:
<?xml version="1.0" encoding="utf-8"?>
<Branch>
<Department>
<Door id="1" enabled="true"/>
<Door id="2" enabled="true"/>
</Department>
<Department>
<Door id="3" enabled="true"/>
<Door id="4" enabled="false"/>
</Department>
<Department>
....
</Branch>
Do you know how could I parse this XML file?
I have been looking for other answers but I could only get it to work till the Department node. It isn't interpreting the Door node level.

Stored procedure to retrieve the query result as XML

Is it possible to retrieve the stored procedure result in XML format? I am using Java to call the stored procedure and Microsoft SQL Server management studio to test my stored procedures. Could someone provide a sample code?
Found something like this
SELECT
CustomerID AS '#CustomerID',
CustName AS '#Name',
(SELECT ProductName AS '#productname'
FROM dbo.Products p
WHERE p.CustomerID = c.CustomerID
FOR XML PATH('Product'), TYPE) AS 'Products',
(SELECT HobbyName AS '#hobbyname'
FROM dbo.Hobbies h
WHERE h.CUstomerID = c.CustomerID
FOR XML PATH('Hobby'), TYPE) AS 'Hobbies'
FROM
dbo.Customers c
FOR XML PATH('Customer'), ROOT('Customers')
Gives following output
<Customers>
<Customer CustomerID="1" Name="Fred">
<Products>
<Product productname="Table" />
<Product productname="Wardrobe" />
<Product productname="Chair" />
</Products>
<Hobbies>
<Hobby hobbyname="Golf" />
<Hobby hobbyname="Swimming" />
</Hobbies>
</Customer>
<Customer CustomerID="2" Name="Sue">
<Products>
<Product productname="CD Player" />
<Product productname="Picture frame" />
</Products>
<Hobbies>
<Hobby hobbyname="Dancing" />
<Hobby hobbyname="Gardening" />
<Hobby hobbyname="Reading" />
</Hobbies>
</Customer>
</Customers>
Is this correct?

fetch the data dynamically from xml

<Point TestFlag="0" id="1" name="Session Introduction">
<PracticeText> </PracticeText>
<PracticeFlag> 0 </PracticeFlag>
<ImageFile gid="1" id="1" name="d002_p001_01" type="png" />
<AudioFile gid="1" id="2" name="d002_ae_p001.mp3" type="mp3" />
</Point>
<Point TestFlag="0" id="1" name="Conversation Introduction">
<PracticeText> </PracticeText>
<PracticeFlag> 0 </PracticeFlag>
<ImageFile gid="1" id="1" name="d002_p001_02" type="png" />
<AudioFile gid="1" id="2" name="d002_ae_p002.mp3" type="mp3" />
</Point>
<Point TestFlag="0" id="1" name="Dialogue sets">
<PracticeText> </PracticeText>
<PracticeFlag> 0 </PracticeFlag>
<ImageFile gid="1" id="1" name="d002_p001_01" type="png" />
<AudioFile gid="1" id="2" name="d002_ae_p001.mp3" type="mp3" />
<ImageFile gid="1" id="1" name="d002_p001_02" type="png" />
<AudioFile gid="1" id="2" name="d002_ae_p002.mp3" type="mp3" />
</Point>
what i basically doing here is i am playing the audio files and displaying the images from the xml which are in AudioFile and ImageFile tag, but first two point i am playing when the user clicks on next button based on the i value.But when it comes to third point i am playing the 2 audio and 2 images continuously.
For playing audio files continuously i am using oncompletion listener.
My problem is
1.I want it do be dynamically acheived.
2.Suppose if we are able to acheive dynamically when in the future one or two more points is inserted in the middle of these three points also it should work
how to do this please help i am stuck
for get the length of each child tag of one point
try this this might help you
NodeList image = doc.getElementsByTagName("Point");
Element firstLevel = (Element)singleTerminalNode;
NodeList value1Nodes = (firstLevel).getElementsByTagName("ImageFile");
lengthofimage = value1Nodes.getLength();

Categories