cancel
Showing results for 
Search instead for 
Did you mean: 

create xml file with values from context

Former Member
0 Kudos

Hi experts!

I am trying to implement a WD application that will have some input fields, the value of those input fields will be used to create an xml file with a certain format and then sent to a certain application.

Apart from this i want to read an xml file back from the application and then fill some other context nodes with values from the xml file.

Is there any standard used code to do this??

If not how can i do this???

Thanx in advance!!!

P.S. Points will be rewarded to all usefull answers.

Edited by: Armin Reichert on Jun 30, 2008 6:12 PM

Please stop this P.S. nonsense!

Accepted Solutions (1)

Accepted Solutions (1)

PradeepBondla
Active Contributor
0 Kudos

Hi,

for reading xml file,

/people/johann.marty/blog/2006/10/03/create-a-web-dynpro-tree-from-an-xml-file

will forward creating xml from context node values soon...

regards,

pradeep

Answers (9)

Answers (9)

Former Member
0 Kudos

Hi Pradeep!!

It's true..Yesterday i thought it was finished..

But in fact the format of the xml was not the way i want it..

I tried to "mess with" the format but i could not. So i thought of using Deepak's code..Still no result...

Any ideas??

Best Regards!

Iria

Former Member
0 Kudos

Try this

I am not able to get you the code through forum; Please provide me your id to post it. Some part of code seems missing after posting.

view my business card for further steps:

Following blog is used to ans this Qst:

/people/sap.user72/blog/2006/05/04/enhancing-tables-in-webdynpro-java-150-custom-built-table-utilities

regards

Vinod V

Edited by: Vinod V on Jul 2, 2008 5:09 PM

Edited by: Vinod V on Jul 2, 2008 5:13 PM

Edited by: Vinod V on Jul 2, 2008 5:16 PM

Edited by: Vinod V on Jul 2, 2008 5:17 PM

Former Member
0 Kudos

Also, event should be declared as a differnet class?????

Thanx!!!

Former Member
0 Kudos

Hi,

Event is a bean class with setter-getter method and i have created object of that as event and passed this as parameter at the time of XML creation.

Regards,

Deepak

Former Member
0 Kudos

Hi Deepak!!

I am currently trying to implement your code, but i still cannot understand what ROOT, ORGANISER and TITLE should be in my web dynpro..

Context attributes????

Thanx in advance!!!!!!

srinivas_sistu
Active Contributor
0 Kudos

Hi,

ROOT, ORGANISER and TITLE

will be the TAG names of your XML File.... Means

Your XML file looks like

<MY XML>

<TAG 1>

- value 1

</TAG 1>

<TAG 2>

-value 2

</TAG2>

</MY XML>

Then ROOT will be "MY XML"

ORGANISER will be "TAG 1"

TITLE will be "TAG 2"

Regards,

Srinivas.

Former Member
0 Kudos

Hi,

i mentioned in the post like they are Tag value of XML and in the view i have created UI with respective context to get the input from the user.

Hope that may help you.

Regards,

Deepak

Former Member
0 Kudos

Pradeep problem solved!!!

THANX!!!!

PradeepBondla
Active Contributor
0 Kudos

Hi Iria,

Yesterday you said its SOLVED.... then what was the problem again?

regards,

Pradeep

Former Member
0 Kudos

Hi Pradeep!!

I tried to use Valery's code but got a lot of errors.

What jar files do i have to add to my WD java build path??

Thanx again!!!!!

PradeepBondla
Active Contributor
0 Kudos

Hi,

What are the errors?

is it with SAX related errors?

Which java version you are using. to import org.xml.sax.* you need to have SAX parser jar but SAX is part of the J2SE 1.4.2.

try to check this

http://www.mail-archive.com/struts-user"AT"jakarta.apache.org/msg96978.html

to show SAX is included in j2se 1.4.2

http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/package-summary.html

still you are getting error with SAX... then you can download it from

http://sourceforge.net/project/showfiles.php?group_id=29449

regards,

Pradeep

Former Member
0 Kudos

Please look at this [WDInteractiveFormHelper.getContextDataAsStream()|http://help.sap.com/javadocs/NW04s/current/wd/com/sap/tc/webdynpro/clientserver/adobe/api/WDInteractiveFormHelper.html#getContextDataAsStream(com.sap.tc.webdynpro.progmodel.api.IWDNode)]. It gives context data in XML form without writing any code!

Cheers,

~kranthi

Former Member
0 Kudos

Hi Pradeep and thank you fro your answer!!!!

My biggest problem is the Java code i have to use to generate and read the xml file, because i used to be an ABAP developer and i am not as familiarized with java..

Any code samples???

That would be extremely useful!!!

Thanx in advance!!!!

Former Member
0 Kudos

Hi,

you need to create three util class for that:-

XMLHandler

XMLParser

XMLBuilder

for example in my XML two tag item will be there e.g. Title and Organizer,and from ur WebDynpro view you need to pass value for the XML tag.

And u need to call buildXML()function of builder class to generate XML, in that i have passed bean object to get the values of tags. you need to set the value in bean from the view ui context.

Code for XMLBuilder:-

/*

  • Created on Apr 4, 2006

  • Author-Anish

  • This class is to created for having function for to build XML

  • and to get EncodedXML

  • and to get formated date

*/

package com.idb.events.util;

import java.text.SimpleDateFormat;

import java.util.Date;

import com.idb.events.Event;

public class XMLBuilder {

/**

  • This attribute represents the XML version

*/

private static final double VERSION_NUMBER = 1.0;

/**

  • This attribute represents the encoding

*/

private static final String ENCODING_TYPE = "UTF-16";

/*Begin of Function to buildXML

  • return: String

  • input: Event

  • */

public String buildXML(Event event) {

StringBuffer xmlBuilder = new StringBuffer("<?xml version=\"");

xmlBuilder.append(VERSION_NUMBER);

xmlBuilder.append("\" encoding=\"");

xmlBuilder.append(ENCODING_TYPE);

xmlBuilder.append("\" ?>");

xmlBuilder.append("<event>");

xmlBuilder.append(getEncodedXML(event.getTitle(), "title"));

xmlBuilder.append(getEncodedXML(event.getOrganizer(), "organizer"));

xmlBuilder.append("</event>");

return xmlBuilder.toString();

}

/End of Function to buildXML/

/*Begin of Function to get EncodedXML

  • return: String

  • input: String,String

  • */

public String getEncodedXML(String xmlString, String tag) {

StringBuffer begin = new StringBuffer("");

if ((tag != null) || (!tag.equalsIgnoreCase("null"))) {

begin.append("<").append(tag).append(">");

begin.append("<![CDATA[");

begin.append(xmlString).append("]]>").append("</").append(

tag).append(

">");

}

return begin.toString();

}

/End of Function to get EncodedXML/

/*Begin of Function to get formated date

  • return: String

  • input: Date

  • */

private final String formatDate(Date inputDateStr) {

String date;

try {

SimpleDateFormat simpleDateFormat =

new SimpleDateFormat("yyyy-MM-dd");

date = simpleDateFormat.format(inputDateStr);

} catch (Exception e) {

return "";

}

return date;

}

/End of Function to get formated date/

}

Code for XMLParser:-

/*

  • Created on Apr 12, 2006

  • Author-Anish

  • This is a parser class

*/

package com.idb.events.util;

import java.io.ByteArrayInputStream;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

import org.xml.sax.XMLReader;

import com.idb.events.Event;

import com.sap.tc.webdynpro.progmodel.api.IWDMessageManager;

public class XMLParser {

/**

  • Enables namespace functionality in parser

*/

private final boolean isNameSpaceAware = true;

/**

  • Enables validation in parser

*/

private final boolean isValidating = true;

/**

  • The SAX parser used to parse the xml

*/

private SAXParser parser;

/**

  • The XML reader used by the SAX parser

*/

private XMLReader reader;

/**

  • This method creates the parser to parse the user details xml.

*/

private void createParser()

throws SAXException, ParserConfigurationException {

// Create a JAXP SAXParserFactory and configure it

SAXParserFactory saxFactory = SAXParserFactory.newInstance();

saxFactory.setNamespaceAware(isNameSpaceAware);

saxFactory.setValidating(isValidating);

// Create a JAXP SAXParser

parser = saxFactory.newSAXParser();

// Get the encapsulated SAX XMLReader

reader = parser.getXMLReader();

// Set the ErrorHandler

}

/**

  • This method is used to collect the user details.

*/

public Event getEvent(

String newsXML,

XMLHandler xmlHandler,

IWDMessageManager mgr)

throws SAXException, ParserConfigurationException, IOException {

//create the parser, if not already done

if (parser == null) {

this.createParser();

}

//set the parser handler to extract the

reader.setErrorHandler(xmlHandler);

reader.setContentHandler(xmlHandler);

InputSource source =

new InputSource(new ByteArrayInputStream(newsXML.getBytes()));

reader.parse(source);

//return the results of the parse

return xmlHandler.getEvent(mgr);

}

}

Code for XMLHandler:-

/*

  • Created on Apr 12, 2006

  • Author-Anish

  • This is a parser class

*/

package com.idb.events.util;

import java.io.ByteArrayInputStream;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

import org.xml.sax.XMLReader;

import com.idb.events.Event;

/*

  • Created on Apr 12, 2006

  • Author-Anish

*This handler class is created to have constant value for variables and function for get events,

  • character values for bean variable,

  • parsing thr date ......etc

*/

package com.idb.events.util;

import java.sql.Timestamp;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.SAXParseException;

import org.xml.sax.helpers.DefaultHandler;

import java.util.*;

import com.idb.events.Event;

import com.sap.tc.webdynpro.progmodel.api.IWDMessageManager;

public class XMLHandler extends DefaultHandler {

private static final String TITLE = "title";

private static final String ORGANIZER = "organizer";

IWDMessageManager manager;

private Event events;

private String tagName;

public void setManager(IWDMessageManager mgr) {

manager = mgr;

}

/**

  • This function is created to get events

*/

public Event getEvent(IWDMessageManager mgr) {

manager = mgr;

return this.events;

}

/*

  • This function is created to get character for setting values through event's bean setter method

*/

public void characters(char[] charArray, int startVal, int length)

throws SAXException {

String tagValue = new String(charArray, startVal, length);

if (TITLE.equals(this.tagName)) {

this.events.setTitle(tagValue);

}

if (ORGANIZER.equals(this.tagName)) {

String orgName = tagValue;

try {

orgName = getOrgName(orgName);

} catch (Exception ex) {

}

this.events.setOrganizer(orgName);

}

}

/*

  • This function is created to parse boolean.

  • */

private final boolean parseBoolean(String inputBooleanStr) {

boolean b;

if (inputBooleanStr.equals("true")) {

b = true;

} else {

b = false;

}

return b;

}

/*

  • This function is used to call the super constructor.

*/

public void endElement(String uri, String localName, String qName)

throws SAXException {

super.endElement(uri, localName, qName);

}

/* (non-Javadoc)

  • @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)

  • This function is used to call the super constructor.

*/

public void fatalError(SAXParseException e) throws SAXException {

super.fatalError(e);

}

/*

  • This function is created to set the elements base on the tag name.

  • */

public void startElement(

String uri,

String localName,

String qName,

Attributes attributes)

throws SAXException {

this.tagName = localName;

if (ROOT.equals(tagName)) {

this.events = new Event();

}

}

public static void main(String a[]) {

String cntry = "Nigeria";

XMLHandler xml = new XMLHandler();

ArrayList engList = new ArrayList();

engList = xml.getCountries();

ArrayList arList = xml.getArabicCountries();

int engIndex = engList.indexOf(cntry);

System.out.println("engIndex :: " + engIndex);

String arCntryName = (String) arList.get(engIndex);

System.out.println(

">>>>>>>>>>>>>>>>>>>>" + xml.getArabicCountryName(cntry));

}

}

Hope that may help you.

If need any help , you are most welcome.

Regards,

Deepak

PradeepBondla
Active Contributor
0 Kudos