Java Utility to Read XML file & Search for Key word

Hi,

We have an requirement to implement a Java utility service as below:

  1. The input to Java utility service is a XML string.
  2. The service should read the XML string and search for a specific field/key in the XML string and give the corresponding value it contains as the result.
    EX:
    XML Data:
    ABC
    XYZ

In java util Search Key=Sender

It should then display as Result=ABC

Could you please let me know if any one has the idea on this.
Please help me ,I need to implement this asap… as I am new to Java Service struggling to implement.

Thanks & Regards,
Kiran

Are you trying to do a key/value pair like the XML properties file read and fetch value from a xml or config file?

Here is the sample JS and modify with your needs:(Purpose: Read a property file and get the values. )

Service Inputs:
properties (string list)
propertyFileName (string)

Service output:
Values (string list)

imports:
java.io.*
java.util.*
java.text.*

/ pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
String properties = IDataUtil.getStringArray( pipelineCursor, "properties" );
String ConfigFileName = IDataUtil.getString( pipelineCursor, "propertyFileName" );
pipelineCursor.destroy();
String result = new String();
String values = null;

if(properties != null)
{
values = new String[properties.length];
String str = new String("");
int count,i = 0 ;
int tokencount;
StringTokenizer propValue = null;
FileInputStream fis = null;
String c= properties;
try
{
fis = new FileInputStream (ConfigFileName);
int size = fis.available ();
byte bytes = new byte [size];
fis.read (bytes);
str = new String (bytes);

     StringTokenizer propName = new StringTokenizer(str,"\n" );
	 tokencount = propName.countTokens();
     String iorPath[] = new String[tokencount];
     while ( propName.hasMoreTokens() ) 
	 {
        	propValue = new StringTokenizer(propName.nextToken(),"=");
        	while ( propValue.hasMoreTokens() ) 
			{
            	String interfaceName = propValue.nextToken();
            	if(c.length > 0 && c.length > i)
				{
					if((interfaceName.trim()).equals(c[i].trim()))
					{
						values[i] = propValue.nextToken().trim();
						i++;
									
					}
				}
					//iorPath[count]   = propValue.nextToken().trim();
        	}//eof while
		}
        		
}
catch (EOFException e) 
{
	result += "End of File Found";
}
catch (IOException e) 
{
	result += "File Not Found" + e.toString();
}
catch (ArrayIndexOutOfBoundsException e) 
{
	result  += "Array Error" + e;
}
catch (Exception e) 
{
	result  += "General Error" + e;
}

}
else
{
values = null;
result += "null values received";
}
// pipeline
IDataCursor pipelineCursor_1 = pipeline.getCursor();

IDataUtil.put( pipelineCursor_1, "values", values );
IDataUtil.put( pipelineCursor_1, "Status", result );
pipelineCursor_1.destroy();

Shared Tab code:

static Date currentDate = new Date();
static SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
static String dateStringer = sdf.format(currentDate);
static String programlogFile = null;
static FileWriter log = null;
// output
static IData output = IDataFactory.create();
static void log(String message)
{
Date currentDate = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
String dateString = sdf.format(currentDate);
System.out.println("inside log else");
System.out.println("inside log after else");
if (programlogFile == null)
programlogFile = "c:\log.txt";
try
{
FileWriter log = new FileWriter(programlogFile, true);
log.write(dateStringer + " " + "\n" + message);
log.write("\r\n");
log.write("\r\n");
log.flush();
log.close();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}

Yes, our requirement is as follows,

  1. Read an XML string
  2. Search for the field called in the XML String
  3. If result==0 then service should return the Originator Value.
    Else
    If result==1 the service should return the message as “No Originator found in the XML”

Please do the needful.

Regards,
Kiran

Try with above code and modify to your needs I am sure this can be done in various ways flow or java based and you can output condition if the out value returns and flag it 0 or not exist (1) logically:

Thanks a lot for your quick response.
Will try to create the JS with provided code & will let you know.

Have a great day…

Regards,
Kiran

Why do you need a Java service for this? You can use built in pub.xml:queryNode service to get value of a particular node in XML.

-Senthil

But how about performance Senthil, because data will be coming in pipeline for few second for real time scenario.

That’s a tricky question… Didn’t knew you are looking for response within few millisecs. With the xml string as input, you would be invoking xmlStringToNode service followed by queryNode service… Check how long it takes to execute in your environment and compare with the total time that is provided to respond for any request… If you have significantly high time to respond, you can go ahead. However, you should also check the time it takes during peak load & average load.

-Senthil

Thank you! Senthil
In the current scenario in our environment its taking only few ms to send the response.
Sure will check & get back to you if any.

Hi,

As I have to read xml string & search for specifc field/tag in the xml string, I tried modifying the code which you have provided as below, and getting output as “Null values received” even after passing the xmlstring & key in the input. Also didnt received any compilation errors

IDataCursor pipelineCursor = pipeline.getCursor();
String[] xmlString = IDataUtil.getStringArray( pipelineCursor, “xmlString” );
String key = IDataUtil.getString( pipelineCursor, “key” );
pipelineCursor.destroy();
String result = new String();
String [] values = null;

if(xmlString != null)
{
values = new String[xmlString.length];
String str = new String("");
int count,i = 0 ;
int tokencount;
StringTokenizer propValue = null;
FileInputStream fis = null;
String[] c= xmlString;
try
{
fis = new FileInputStream (key);
int size = fis.available ();
byte[] bytes = new byte [size];
fis.read (bytes);
str = new String (bytes);

StringTokenizer propName = new StringTokenizer(str,"\n" );
tokencount = propName.countTokens();
String iorPath[] = new String[tokencount];
while ( propName.hasMoreTokens() )
{
propValue = new StringTokenizer(propName.nextToken(),"=");
while ( propValue.hasMoreTokens() )
{
String interfaceName = propValue.nextToken();
if(c.length > 0 && c.length > i)
{
if((interfaceName.trim()).equals(c[i].trim()))
{
values[i] = propValue.nextToken().trim();
i++;

}
}
//iorPath[count] = propValue.nextToken().trim();
}//eof while
}

}
catch (EOFException e)
{
result += “End of File Found”;
}
catch (IOException e)
{
result += “File Not Found” + e.toString();
}
catch (ArrayIndexOutOfBoundsException e)
{
result += “Array Error” + e;
}
catch (Exception e)
{
result += “General Error” + e;
}

}
else
{
values = null;
result += “null values received”;
}
// pipeline
IDataCursor pipelineCursor_1 = pipeline.getCursor();

IDataUtil.put( pipelineCursor_1, “values”, values );
IDataUtil.put( pipelineCursor_1, “Status”, result );
pipelineCursor_1.destroy();

Sample Input XMLstring & key used:

XmlString:

<?xml version="1.0"?> Kiran, Kim Midnight Rain Fantasy 5.95 2000-12-16

Key:

Hi RMG,

I am waiting for your response, can you please check the below code & correct if I have modified anything incorrect.

Request to help out.

Sorry it works only when .properties is like…so it needs to be fine tuned for xml based:

book=author

key = book
value = author

Sorry , as I dont know JS, if you dont mind can you please modify the code as per my requirement which I have mentioned in the above chain & provide me.

As I need this to be implemented very urgently… request you to please modify if possible.

How to migrate from wm6.5 to 7.1 please help me.

Please review the webMethods Upgrade documentation that should cover the topics.

http://techcommunity.softwareag.com/ecosystem/documentation/webmethods/wmsuites/wmsuite7/Cross_Product/webMethodsUpgradeGuides.pdf

HTH,
RMG