Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Friday, April 24, 2020

Fetching Unique XML Tags based on Values

Listing down the unique values of a tag in entire XML
Below is the raw information in the form of an XML

<?xml version="1.0" encoding="UTF-8"?>
<STATES>
<STATE>
<NAME>TELANGANA</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>HYDERABAD</CAPITAL>
</STATE>
<STATE>
<NAME>TAMILNADU</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>CHENNAI</CAPITAL>
</STATE>
<STATE>
<NAME>KARNATAKA</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>BENGALURU</CAPITAL>
</STATE>
<STATE>
<NAME>KERALA</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>TRIVANDRUM</CAPITAL>
</STATE>
<STATE>
<NAME>MAHARASHTRA</NAME>
<REGION>WEST</REGION>
<CAPITAL>MUMBAI</CAPITAL>
</STATE>
<STATE>
<NAME>TELANGANA</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>HYDERABAD</CAPITAL>
</STATE>
<STATE>
<NAME>TAMILNADU</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>CHENNAI</CAPITAL>
</STATE>
</STATES>

I want to get only the unique state names from the above information. (TELANGANA,TAMILNADU,KARNATAKA,KERALA,MAHARASHTRA)


//STATE/NAME[not(. = ../following-sibling::STATE/NAME)]

Credits : 
Padmapriya Iyengar (padmapriya.iyengar@gmail.com)

Thursday, January 29, 2015

XPath for the node with maximum value for a particular element : XPath1.0

Here is the list of bond characters played in bond movies and their earnings from movies,

<JamesBondCharacters>
<JamesBond>
<Name>Sean Connery</Name>
<Earnings>40000</Earnings>
<Currency>GBP</Currency>
</JamesBond>
<JamesBond>
<Name>Roger Moore</Name>
<Earnings>55000</Earnings>
<Currency>GBP</Currency>
</JamesBond>
<JamesBond>
<Name>George Lazenby</Name>
<Earnings>20000</Earnings>
<Currency>GBP</Currency>
</JamesBond>
<JamesBond>
<Name>Timothy Dalton</Name>
<Earnings>25000</Earnings>
<Currency>GBP</Currency>
</JamesBond>
<JamesBond>
<Name>Pierce Brosnan</Name>
<Earnings>70000</Earnings>
<Currency>GBP</Currency>
</JamesBond>
<JamesBond>
<Name>Danial Craig</Name>
<Earnings>30000</Earnings>
<Currency>GBP</Currency>
</JamesBond>
</JamesBondCharacters>

From this list I want to get the Bond details who earned the highest and least during his time. This example is just taken for fun to understand the XPath. 

To get the highest earned bond details use the below XPath

//JamesBond[not(./Earnings<//JamesBond/Earnings)]

To get the least earned bond details use the below XPath

//JamesBond[not(./Earnings>//JamesBond/Earnings)]

Note : The XPath which we used here is valid in XPath1.0. Alternatively you can check the same here in the below link

http://www.online-toolz.com/tools/xpath-editor.php

Credits :
GIB Learnings from Anitha and Padma Priya Iyengar

Wednesday, July 16, 2014

Listing down unique tags in entire XML

I have a requirement here, I would try to keep it in very simple terms,

I have a raw information in the form of XML:

<MyFruits>
<Apple>23</Apple>
<Mango>12</Mango>
<Orange>10</Orange>
<Apple>19</Apple>
</MyFruits>

I want to get only unique fruits among them. (Apple,Mango and Orange)

Can anyone write an XPath to retrieve this?
Status : Archived

You can give your answers as comments

Hint : 
Had the XML be like below

<MyFruits>
<Apple>23</Apple>
<Apple>19</Apple>
<Mango>12</Mango>
<Orange>10</Orange>
</MyFruits>

This XPath would have worked 

//MyFruits/*[not(name(.)=name(following-sibling::*))]


Wednesday, May 28, 2014

Call a web service from HTML

Goal :
The goal for this post is to know how exactly we call a web service in html page (Service and HTML should be hosted in the same server).

Pre-Requisites :

  • There should be a web service which is hosted in the server. 
  • One should have knowledge on what are the parameters to be sent to call a web service or at least know how to use a SOAP UI to call a web service.
  • Basic html knowledge.
Explanation :
  • Know the web service to be called
You have to know the parameters of the web service to be called. Below are the parameters which are to be known (Assuming the web service is in the same server where the html file is being developed),
1. End point
2. SOAP Request
3. Content Type

If the above parameters are not known these can be taken from soap UI or from Fiddler as shown below

Below picture gives a brief idea on how to get End Point and SOAP Request from SOAP UI



Below picture gives information on how to get End Point, SOAP Request and Content Type from Fiddler.


There are other ways of getting these information like network tab of chrome's developer tools etc., These are just few ways of getting these information. However the goal here is to get the parameters not how do we get. Once we have this information we go ahead by writing simple html page.
  • Write a simple HTML Page
Below is the sample page how do we write it.

  • Write a function which calls a web service and display it on the browser

function soap() {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open('POST', 'http://dhl-hyd1024/cordys/com.eibus.web.soap.Gateway.wcp?', true);
            // build SOAP request
            var sr =             
                '<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">'+
'<SOAP:Body>'+
                        '<sty:GetXMLObject xmlns:sty="http://schemas.cordys.com/1.0/xmlstore">' +
                            '<sty:key version="isv">/OAuthIntegration/RedirectorContent.xml</sty:key>' +
                        '</sty:GetXMLObject>' +
' </SOAP:Body>'+
'</SOAP:Envelope>';
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
            xmlhttp.send(sr);
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {
document.getElementById('responseXML').innerHTML = xml_to_string(xmlhttp.responseXML);
                        alert('done use firebug to see response');
                    }
                }
            }
        }

Note : xml_to_string here is a function which is irrelevant here.

Highlighted sections are variable parameters (important variable parameters). These parameters should be replaced by the parameters what we have collected in the previous sections.

However this way of communication is not JQuery based which I would suggest to use in order to be in recent industrial terms.

Thursday, January 16, 2014

Read content from XML File and write into EXCEL

package com.DH.Explorations;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import com.eibus.xml.nom.Document;
import com.eibus.xml.nom.Node;
import com.eibus.xml.nom.XMLException;
import com.eibus.xml.xpath.XPath;

public class WriteExcel {

public static void main(String[] args) throws IOException, XMLException {
// TODO Auto-generated method stub
HSSFWorkbook workbook=new HSSFWorkbook();
Document doc = new Document();
int configXML=0;
configXML = doc.load("D:\\bFOIntegrations\\WorkOrderInfo.xml");
System.out.println(Node.writeToString(configXML, true));
HSSFSheet Sheet=workbook.createSheet("WorkOrderRecords") ;
int []workOrderItems = XPath.getMatchingNodes(".//WorkItemRecord", null, configXML);
for(int i=0;i<workOrderItems.length;i++)
{
Row row = Sheet.createRow(i);
String bFO = Node.getAttribute(XPath.getFirstMatch(".//Keys", null, workOrderItems[i]), "bFO");
String BO = Node.getAttribute(XPath.getFirstMatch(".//Keys", null, workOrderItems[i]), "BO");
String Status = Node.getDataElement(workOrderItems[i], "Status", "");
String statusMsg =Node.getDataElement(workOrderItems[i], "StatusMsg", "");
Cell cell_0 = row.createCell(0);
Cell cell_1 = row.createCell(1);
Cell cell_2 = row.createCell(2);
Cell cell_3 = row.createCell(3);
cell_0.setCellValue(bFO);
cell_1.setCellValue(BO);
cell_2.setCellValue(Status);
cell_3.setCellValue(statusMsg);
}
File fileDirectory;
String FilePath="D:\\temp";
fileDirectory = new File(FilePath);
if(!fileDirectory.exists())
fileDirectory.mkdirs();
FilePath=FilePath+"\\"+(new Date().getTime())+".xls";
FileOutputStream out = new FileOutputStream(new File(FilePath));
workbook.write(out);
}
}

Tuesday, December 17, 2013

XPath : Select Unique in XML

XPath.getMatchingNodes("descendant-or-self::HMT_PROCESS_CONFIG[not(PROCESS_NAME=preceding::HMT_PROCESS_CONFIG/PROCESS_NAME)]/PROCESS_NAME", null, processData);