Creating an XML file

Hello All,
I’m fairly new to WM, so any kind of help would be greatly appreciated!

I have a service which returns results that I need to put into an xml file. So basically I have data that I need to put into an xml file. How do I create an xml file?
Thank you

The easiest way would be to create a record from the target DTD or schema. Then you can map you data into a record reference and then simply call pub.web:recordToDocument

hi;
i want to actually maintain a xml based property file in the B2B server.I want the user to be able to add/delete/view the properties in this XML based file.
Whats the best way of doing it ?
thanx…

Hey Ak,

Theo gave you good information. However, to write the file, you will need to create a simple writeFile java service.

Here is one for you to help with this task:

INPUT:

T - fileContents
T - fileName (this includes full path /usr/home/yourfile.txt)

OUTPUT:

T - serviceMessage

Possible serviceMessages:

WF-01 - File Written
WF-02 - Error

Imports:
java.io.*
java.lang.*
java.util.*
java.text.*

(you can probably get rid of some of these imports, but I need
them because they are shared)

Here’s the code:

// fileContents - String contents of file.
// fileName - name of file you want to write.

IDataHashCursor idhc = pipeline.getHashCursor();
idhc.first( “fileContents” );
String fileContents = (String) idhc.getValue();
idhc.first( “fileName” );
String fileName = (String) idhc.getValue();
idhc.destroy();

String serviceMessage = “”;

try {

char buffer = new char[fileContents.length()];
fileContents.getChars(0, fileContents.length(), buffer, 0);

FileWriter fw = new FileWriter(fileName,true);

fw.write(buffer);

fw.close();

serviceMessage = “WF-01”;

} catch (Exception e) {

serviceMessage = “WF-02”;

}

// output pipeline

// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
IDataUtil.put( pipelineCursor, “serviceMessage”, serviceMessage );
pipelineCursor.destroy();

There may be a better way to write this code, but this has worked
for me for over two years without any problems.

I wanted to separate the posts to answer the second question of how to provide a way for the users to make changes.

Currently, webMethods supports DSP or Dynamic Server Pages. There is a manual that provides an overview. I have found them to be a little difficult and inflexible to use.

If you decide to use DSPs, then I would divide the Profile up by sections and allow changes. I currently run with an XML-based profile and have found it to work very well. Making changes, however, are arduous and I use XML spy or notepad for simple changes. My xml profile is based on a DTD and I create an IData record in webMethods based on the DTD. On startup, my server loads my file-based XML profile into a data store and prefetches the results for performance.

If you are good with JSPs and have a servlet container running already someplace in your architecture, there are several very good ways to do this and many various implementations. With the new product line coming close to release, it may be a way to go, since it is believed that 6.0 will support JSPs.

If you have database support (I DO NOT), then the database is probably the best way to go. On startup, you can query the database, retrieve the profile data and store it in a data store.

If you are unfamiliar with data stores (repository) in webMethods Integration Server, check out the built-in services guide for pub.storage, or check this site for the articles that cover this topic.

Hope this helps.

Ray

Hey Ak,

if u want a property file functionality you can have your own user defined cnf file, which will be referred using key-value pair.A sample is provided in property folder of wmSamples package.
This is easier to use and configure.
Hope this gives you a start…

Prabhu

I am trying to use JDOM (www.jdom.org) to parse and validate XML in a Java Service. However, I get a Null Pointer Exception …(as shown below):

---- code snippet ----

Values errorRecord = new Values();
IDataCursor cursor = pipeline.getCursor();
XMLOutputter out = new XMLOutputter();

try {
if (cursor.first(“inboundXML”)) {
String inputXML = (String) cursor.getValue();

        }    
        builder = new SAXBuilder(); 

try {

                 if (builder != null) { 

*** NPE *** doc = builder.build(new StringReader(inputXML));
isValid = true;
out.output(doc, System.out);
}
} catch (JDOMException jdome) {
// assert problems associated with a
}
---- code snippet ----

I have the jars for jdom and the xerces parser in the [Server]/lib/jars … so that they show up in the classpath correctly. However, I am stumped about the reason I am getting this NPE. The inputXML string is NOT null. I have verified it. The builder object is NOT null, I have checked that as well. But the builder.build(…) call throws an NPE.

The IDE version of this code, runs fine without any problem. I am having problems when running inside wM.

I was hoping anyone of you out there would have some insight of what am I doing wrong.

Thank you very much for your help.

exciton:

Is there some reason why the built-in facilities for XML handling and validation do not meet your needs? XML is what Integration Server lives for and you may be hurting its feelings by taking its job away. :wink:

I agree with Rob and will quote a Turkish Proverb that my 9th grade English teacher once put on the black board:

No matter how far you have gone down the wrong road, turn back.
Drop your JDOM stuff and use the Integration Server’s Built-In Services. Be sure to ask if you need some help getting started.

Hi Everyone;

I understand what some people have suggested about creation of an XML based property file.
However, i do believe that for creation/maintaining this kind of a property/config file, one would definitely have to use parsing(JAXP) etc.
This is bcoz its not just a matter of creating a file offline and accessing it;but a matter of being able to add/list/delete properties from that file using Developer real time.The suer shud be able to develop services in Developer such as addProperty, DeleteProperty,ListProperties etc.so that the he can manipulate the file as and when desired.
This kind of a structure and capabilities, i feel can only be done using JAXP and not inbuilt IS services.
Please let me know if someone has a different thought and a sample to do this !!
thanx…

exciton - at first glance this looks suspect:
try {
if (cursor.first(“inboundXML”)) {
String inputXML = (String) cursor.getValue();

}

You’re declaring inputXML inside a code block, so it is only local to that code block. That may be the cause of your NPE. Try this:

String inputXML =new String();
try {
if (cursor.first(“inboundXML”)) {
inputXML = (String) cursor.getValue();
}

“The suer shud be able to develop services in Developer such as addProperty, DeleteProperty,ListProperties etc.so that the he can manipulate the file as and when desired.”

You can do this. Each service would load the file using built-in services, do the desired operations, and then save the file if needed. You’ll want to implement a locking facility so that only one person can edit at a time. JAXP is definitely not necessary in this case. IS has all the services and capability you need to do what you describe (IS uses DOM/SAX internally).

Good catch on the code problem Theo. The service should have failed to compile at the line:

doc = builder.build(new StringReader(inputXML));

inputXML is undefined. Is there more to the code exciton?