cancel
Showing results for 
Search instead for 
Did you mean: 

Reading Contents from an XML document

Former Member
0 Kudos

Hi,

I have an XML document (XYZ.xml) in a directory called DRT.

I have created a alias to this folder DRT in HTTPProvider in my Portal Server through Vis Admin. I am able to access this XYZ.xml file directly through this URL.

<u>http://<Server>:50000/DRT/XYZ.xml</u>

Now i want to read this XML file content through Webdynpro.

Is there any API in WebDynpro Runtime to read this XML file content ?

Please give me some sample code or actual API details to read the XML file contents.

Thanks

Senthil

Accepted Solutions (1)

Accepted Solutions (1)

former_member182372
Active Contributor
0 Kudos

Hi Senthil,

To read data from URL check this post

To parse the file you need Java API For XML Processing (JAXP) (http://java.sun.com/webservices/jaxp/docs.html http://help.sap.com/saphelp_webas630/helpdata/en/36/ef353e39011a38e10000000a114084/frameset.htm). For example you can use SAX. What you need is to implement ContentHandler. As example you can use this http://java.sun.com/j2ee/1.4/docs/tutorial/examples/jaxp/sax/samples/Echo01.java

To replace some text inside element you should change implementation of characters(char[] buf, int offset, int len) method in mentioned example.

Check this too

Best regards, Maksim Rashchynski

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Senthilkumar,

_*When there is an error , corresponding error page should be displayed instead of whole stack trace.

*_I want to achieve this with CE 7.1 EHP1.

To cary out this I am not finding HTTP property ServiceUnavailableResponse and Webcontainer property ApplicationStoppedFile in nwa system configuration.

Please can you help to know how to get this properties in nwa .

regards

Mahesh

former_member335005
Participant
0 Kudos

Hi Senthil,

This simple SAX example might be useful. I usually copy paste this copy and make changes whereever required.

import org.xml.sax.*;

import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParserFactory;

import javax.xml.parsers.SAXParser;

public class SaxTest1 extends DefaultHandler

{

public static void main(String[] args)

{

try

{

SAXParserFactory factory = SAXParserFactory.newInstance();

SAXParser saxParser= factory.newSAXParser();

DefaultHandler handler = new SaxTest();

factory.setValidating(true);

System.out.println("File Name "+ args[0]);

System.out.println("before parse");

saxParser.parse(args[0],handler);

System.out.println("after parse");

}

catch (Exception e)

{

e.printStackTrace();

}

}

public void startDocument() throws SAXException

{

System.out.println("Document Started");

}

public void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, Attributes attributes)throws SAXException

{

System.out.println("Element Started");

}

public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName) throws SAXException

{

System.out.println("Element ended");

}

public void endDocument() throws SAXException

{

System.out.println("Document Ended");

}

public void characters(char[] ch, int start, int length) throws SAXException

{

System.out.println("Character");

}

}

Hope that helps.

Regards,

Sangeeta

Former Member
0 Kudos

Hi,

Thanks for the help.

Regards

Senthil

Former Member
0 Kudos

Hi Senthil ,

I don't think we have any APIs in webdynpro runtime to read XML.

What you can do is write your own XML parser class(DOM/SAX) and parse the XML .

Regards, Anilkumar

Former Member
0 Kudos

Hi,

I am not trying to parse, aim was just to read the contents of the whole file and move it to a string.

Ok, if i have text file in that folder, can i read it.

I am trying to read it usign this code.

byte[] bytArray = new byte[4096];
URL fileURL = new URL("http://<server>:<port>/GenDocs/Test.txt");
InputStream inpStream = fileURL.getInputStream();
inpStream.read(bytArray);
if(bytArray != null)
{
   String contents = bytArray.toString();
}

It is returning some junk characters.

Thanks

Senthil