Stored procedure to retrieve the query result as XML - java

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?

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.

XMLBeam parsing

I am unable to properly read a XML file with XMLBeam:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ns1:storeTokenResponse xmlns:ns1="http://recurring.services.adyen.com">
<ns1:result>
<additionalData xmlns="http://recurring.services.adyen.com" xsi:nil="true" />
<alias xmlns="http://recurring.services.adyen.com">ALIAS</alias>
<aliasType xmlns="http://recurring.services.adyen.com">Default</aliasType>
<params xmlns="http://recurring.services.adyen.com" xsi:nil="true" />
<pspEchoData xmlns="http://recurring.services.adyen.com" xsi:nil="true" />
<pspReference xmlns="http://recurring.services.adyen.com">PSPREFERENCE</pspReference>
<recurringDetailReference xmlns="http://recurring.services.adyen.com">RECURRINGDETAILREFERENCE</recurringDetailReference>
<redirectType xmlns="http://recurring.services.adyen.com" xsi:nil="true" />
<redirectUrl xmlns="http://recurring.services.adyen.com" xsi:nil="true" />
<result xmlns="http://recurring.services.adyen.com">RESULT</result>
</ns1:result>
</ns1:storeTokenResponse>
</soap:Body>
with given Java class:
public interface PaymentResponse {
#XBRead("/soap:Envelope/soap:Body/ns1:storeTokenResponse/ns1:result/ns1:alias")
String getAlias();
#XBRead("/soap:Envelope/soap:Body/ns1:storeTokenResponse/ns1:result/ns1:recurringDetailReference")
String getRecurringDetailReference();
#XBRead("/soap:Envelope/soap:Body/ns1:storeTokenResponse/ns1:result/ns1:pspReference")
String getPspReference();
#XBRead("/soap:Envelope/soap:Body/ns1:storeTokenResponse/ns1:result/ns1:result")
String getResult();
}
I was doing it like this :
final PaymentResponse paymentResponse = new XBProjector.onXMLString(xml).read(PaymentResponse.class);
Assert.assertTrue("ALIAS",paymentResponse.getAlias());
And yes, proper XPath is ns1:alias, as I've tested.
But looks like XMLBeam is not properly recognizing the values.
Can anyone help, please ?

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.

Parsing XML with object reference creation

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
}

IBM Jazz Team Server: Getting Changes from Changeset using OSLC

I am using the following query to retrieve the Changeset.
OSCL Query:
https://ibm.com:9443/ccm/resource/itemOid/com.ibm.team.scm.ChangeSet/_HFxrmiEbEeS7m-qENunxUw?_mediaType=text/xml
Getting the following response:
<scm:ChangeSet itemId="_HFxrmiEbEeS7m-qENunxUw"
properties="">
<stateId>_W8aNECEbEeS7m-qENunxUw</stateId>
<immutable>true</immutable>
<contextId>_Dp6kMdwTEd2jUupDpQV1Rw</contextId>
<modified>2014-08-11T05:50:11.568Z</modified>
<mergePredecessor xsi:nil="true" />
<predecessor>_RaxwgCEbEeS7m-qENunxUw</predecessor>
<active>false</active>
<comment>Test</comment>
<lastUpdatedDate>2014-08-11T05:50:11.225Z</lastUpdatedDate>
<modifiedBy itemId="_FAv8gA4GEeSsZdvBzERdWQ" />
<changes>
<internalId>_HUW6IiEbEeS7m-qENunxUw</internalId>
<kind>2</kind>
<before>_nDuHQBhzEeS88s3Rov8pNA</before>
<after>_HUNJJSEbEeS7m-qENunxUw</after>
<item itemId="_k5S2oBhzEeSht4FX-gq3Zg" xsi:type="filesystem:FileItemHandle" />
</changes>
<component itemId="_RvxoEBhzEeS88s3Rov8pNA" />
<owner itemId="_FAv8gA4GEeSsZdvBzERdWQ" />
<xComponentLink xsi:nil="true" />
I would like to get the Changes detail. It would be great if someone shares the OSLC
query to retrieve Changes details.
#user2530633, try the below query, it is not oslc query
.com/ccm/service/com.ibm.team.filesystem.common.internal.rest.IFilesystemRestService2/changeSetPlus?knownTextContentTypePrefixes=text%2F&knownTextContentTypeStrings=application%2Fxml&knownTextContentTypeStrings=application%2Fjavascript&knownTextContentTypeStrings=application%2Fecmascript&knownTextContentTypeStrings=application%2Fx-javascript&changeSetPath=workspaceId%2F_Dh7kMAHIEeSZzL8CZgHcOQ%2FchangeSetId%2F_quVzDljgEeSiBMylK1kVWg&maxChanges=500"
here just replace your workspaceid and changesetid you can find the changes with the content

Categories