How to convert json array to xml array for post request? - arrays

I have following post data for a JSON request which is working fine.
"customer"=>{"subdomain"=>"Test", "firstname"=>"john",
"lastname"=>"doe", "email"=>"john.doe#example.com",
"company"=>"Sample", "default_language"=>"en",
"active_modules"=>["cmdb", "evm", "itil"]}
I have also enabled XML for my server, so want to respond to XML post requests too. I tried to convert above JSON data to XML but it's not as expected.
<customer>
<active_modules>
<element>cmdb</element>
<element>evm</element>
<element>itil</element>
</active_modules>
<company>Sample</company>
<default_language>en</default_language>
<email>john.doe#example.com</email>
<firstname>john</firstname>
<lastname>doe</lastname>
<subdomain>Test</subdomain>
</customer>
Problem is with array element. How can I convert this array element to pass exactly same data as JSON requests to server?

For an array, you need to pass same node multiple times.
<customer>
<active_modules>cmdb</active_modules>
<active_modules>evm</active_modules>
<active_modules>itil</active_modules>
<company>Sample</company>
<default_language>en</default_language>
<email>john.doe#example.com</email>
<firstname>john</firstname>
<lastname>doe</lastname>
<subdomain>Test</subdomain>
</customer>

Related

Having an issue with XML containing nested objects to POCO (de)serialisation (VB.Net)

I've created an SP in SQL Server that returns as XML. I decided to do this as the information has contacts and addresses in it and I wanted to reduce the amount of data I get.
<Accounts>
<Account>
<Company />
<ContactNumber />
<Addresses>
<Address>
<Line1 />
....
</Address>
<Addresses>
<Contacts>
<Contact>
<Name />
....
</Contact>
<Account>
</Accounts>
I have found SqlCommand.ExecuteXmlReader but I'm confused as to how to serialise this into my POCO. Can someone point me at what my next step is. (The POCO was created by the Insert XML as a class menu item in VS2019).
My Google fu is letting me down as I'm not sure what I should be looking for to help understand how to serialize the XML into something that will allow me to go with Foreach Account in AccountsClass type logic.
Any help is greatly appreciated.
PS The XML above is just a sample to show what I'm doing. The actual data is over 70 fields and with two FK joins the initial 40000 rows is well in excess of 1.8 million once selected as a normal dataset.
EDIT: Just in case someone stumbles on this and are in the same situation I was in.
When preparing a sample record for the Past XML to class make sure you have more than one record if you are expecting something similar to my example above. (The class changes to support more than one record.)
You get very different results when searching for deserialize when doing your research. This small change resulted in me figuring out the issue.

How to log the required part of a file using apache camel.?

How to log part of the incoming data in the console and log file?
Suppose, I am getting input from File source and It contains the Sales Order. I want to log all the Sales Order number.
Input :
<root>
<records>
<salesorderno> 10002<salesorderno>
</records>
<salesorderno> 10005<salesorderno>
<records>
</records>
<records>
<salesorderno> 10032<salesorderno>
</records>
</root>
You could use an XPATH expression to get the values. Something like:
from("file:inbox")
.setHeader("salesordernos", xpath("/root/records/salesorderno"))
.to("log:loggerName?showBody=true&showHeaders=true");

How to retrieve multiple XML siblings - SQL Server

I have searched thoroughly and not found any examples or guidance on how I can query and retrieve the values of multiple XML siblings.
There are plenty of examples on how to retrieve a single sibling value.
For example given the following XML fragment:
<StoreSurvey>
<AnnualSales>800000</AnnualSales>
<AnnualRevenue>80000</AnnualRevenue>
<BankName>United Security</BankName>
<BusinessType>BM</BusinessType>
<YearOpened>1996</YearOpened>
<Specialty>Mountain</Specialty>
<SquareFeet>21000</SquareFeet>
<Brands>2</Brands>
<Internet>ISDN</Internet>
<NumberEmployees>13</NumberEmployees>
<Products Type="Bikes">
<Product>Mountain</Product>
<Product>Road</Product>
<Product>Racing</Product>
</Products>
<Products Type="Clothes">
<Product>Jerseys</Product>
<Product>Jackets</Product>
<Product>Shorts</Product>
</Products>
</StoreSurvey>
If I want to retrieve the value of <AnnualSales>, I can execute the following statement in query analyzer:
SELECT Survey_untyped.query('/StoreSurvey/AnnualSales')
FROM Stores;
The result set will properly display
<AnnualSales>800000</AnnualSales>
However, what if I want to retrieve both <AnnualSales> AND <AnnualRevenue>? How would I do that?
The query should provide a result set looking like:
<AnnualSales>800000</AnnualSales>
<AnnualRevenue>80000</AnnualRevenue>
Or what if I want three of the sibling values “BankName” in addition to those values? Result set would look like this:
<AnnualSales>800000</AnnualSales>
<AnnualRevenue>80000</AnnualRevenue>
<BankName>United Security</BankName>
Does anyone know the answer to that?
This XPath, which uses the self:: axis,
/StoreSurvey/*[self::AnnualSales or self::AnnualRevenue or self::BankName]
will select
<AnnualSales>800000</AnnualSales>
<AnnualRevenue>80000</AnnualRevenue>
<BankName>United Security</BankName>
from your XML, as requested.

GeoServer WFS PropertyIsEqualTo not matching trailing whitespace

I currently have a dataset in a postgis database that contains trailing whitespaces for certain columns e.g. TOWNSHIP.
When a WFS request is made through geoserver's WFS functionality, no data is returned (see below)
<?xml version="1.0"?>
<wfs:GetFeature maxFeatures="10" startIndex="0" service="WFS" version="1.1.0" outputFormat="json" xmlns:wfs="http://www.opengis.net/wfs" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd">
<wfs:Query typeName="<layer>">
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<ogc:And>
<PropertyIsEqualTo>
<PropertyName>TOWNSHIP</PropertyName>
<Literal>Town </Literal>
</PropertyIsEqualTo>
</ogc:And>
</ogc:Filter>
<SortBy xmlns:ogc="http://www.opengis.net/ogc">
<SortProperty>
<PropertyName>NUM</PropertyName>
<SortOrder>ASC</SortOrder>
</SortProperty>
</SortBy>
</wfs:Query>
</wfs:GetFeature>
In the above GetFeature request, the WFS is trying to match by TOWNSHIP where the value is 'Town '. There are records in the database with the value 'Town '.
I could use the PropertyIsLike filter but that would match extra records.
Could someone please let me know if you have come across this issue and how to resolve it?
Thanks
Amar

libxml xmlXPathEvalExpression order

I've started using libxml in C, and I'm using the xmlXPathEvalExpression function to evaluate XPath.
My XML file actually represents a table, with each child node representing a row in that table and its attributes are the corresponding values, so the order is important.
I couldn't find information about that function in regards to its order. Does it return the nodes in the document order
for example, the following xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<TABLE0>
<Row Item0="1" Item1="2"/>
<Row Item0="2" Item1="3"/>
<Row Item0="3" Item1="4"/>
</TABLE0>
<TABLE1>
<Row Item0="1" Item1="12"/>
<Row Item0="6" Item1="15"/>
</TABLE1>
</Root>
Can I be sure that after evaluating /Root/TABLE0/* and getting the nodeset, calling nodeSet->nodeTab[0] would get the first row, nodeSet->nodeTab[1] would get the second and so on?
If not, is there a way to sort it by document order?
Thanks
Yes, the results of XPath expressions evaluated with xmlXPathEvalExpression or compiled with xmlXPathCompile are always sorted in document order. (Internally, they're compiled by calling xmlXPathCompileExpr with a true sort flag.)

Resources