cancel
Showing results for 
Search instead for 
Did you mean: 

How use XML-Parser in Web Dypro (how get filename)

Former Member
0 Kudos

Hi everybody,

i want to use the saxparser to read a xml-file via a Fileupload-UI element. In Web Dynpro it is not possible to get the path of a file which was on a client through the Fileupload-UI. So how can i use a xml-parser when i have to give it the file name?

Is it possible to use the "resource" of the Fileupload-UI instead ot the filename? How can i call the parser?



                    SAXParser saxParser= null;
		
	try{    			
		SAXParserFactory factory = SAXParserFactory.newInstance();			
		saxParser = factory.newSAXParser();			
		saxParser.parse( new File(??????), new  Gaeb());

	}catch (Exception ex){
		ex.printStackTrace();
	} 

regards,

Sid

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Why do you need the path on the client after uploading? After uploading the file to the server, you can get its URL via IWDResource.getUrl() and you have a read() method for accessing the resource.

Armin

Former Member
0 Kudos

Hi Armin,

you mean i have to upload the file to a KM folder e.g. /documents first and than get the path of the file?

regards,

Sid

siarhei_pisarenka3
Active Contributor
0 Kudos

Hi Sid

>you mean i have to upload the file to a KM folder e.g. /documents first and than get the path of the file?

He means that FileUpload control should be bound to context attribute with type IWDResource. When the control uploads a file on the server it'll store it internally on the server and the IWDResource will contain a reference to the uploaded file. IWDResource allows to get a path or even URL to the temporary stored file. And you can use the path for XML parsing.

Read also help about the FileUpload control.

BR, Siarhei

Former Member
0 Kudos

Hi Siarhei,

i bind the resource property to my context of type IWDResource, the xml-file is now uploaded to KM-Content : /documents/GSW/Upload_UT_Sample_A.xml.

I create with resource.getURL() the path of the xml-file and give it to my xml-parser class, but i cannot get any value

from my parser (value returns "null"). Now i don't know if i have a mistake in creating the path of my file or a mistake in my parser??

Could you please have a look to my source code:

I try to get the path with:


       /* Get Path of "/documents/GSW/XML/Upload_UT_Sample_A.xml" */
       String urlToFile = resource.getUrl(WDFileDownloadBehaviour.AUTO.ordinal());		   	

      /* Call the Parser Class with path*/
      MyXMLParser myParser= new MyXMLParser ( urlToFile );

      /* Get one value from xml-class*/
      wdComponentAPI.getMessageManager().reportSuccess("Value: " + myParser.getValue());  <-- returns "null"	

My XML-Class:



public class MyXMLParser extends DefaultHandler {
	
	private String path = null;
	private String fName = null;
	public  String lValue;	
	private boolean lSupplier = false;
	private boolean lQuoteNumber = false;	
		
	/* */
	public Gaeb(){ }
		 
	/* */	
	public Gaeb(String path){
				
		SAXParserFactory factory = null;					
		SAXParser saxParser = null;
			
		try { 
			factory = SAXParserFactory.newInstance();					
			saxParser = factory.newSAXParser();			
			saxParser.parse( path , new MyXMLParser () );	<-- i try also new File(path) instead of path.		
															
		}	catch (Exception ex){			
			ex.printStackTrace();
		} 	
	}	
	
    /* */
	public void startElement(String uri, String localName,
		String qName, Attributes attributes)
		throws SAXException {	 
	 
		lSupplier = true;   	 
	}			
		 
	/* */	 				
	public void characters(char[] ch, int start, int length)
		throws SAXException {
			
		if (lSupplier){
			this.lValue = new String (ch,start,length);
		}
	}
	
	/* */
	public String getValue(){
		return this.lValue;
	}
}

regards,

Sid

Edited by: Sid on Dec 2, 2009 2:47 PM

Former Member
0 Kudos

Hi,

When you create a context attribute of type Resource and bind it to the resource property of ileuplod UI element which will return you the Input stream, you can give the Input stream as input to sax parser parse method.

SAXParser saxParser= null;

try{

SAXParserFactory factory = SAXParserFactory.newInstance();

saxParser = factory.newSAXParser();

saxParser.parse( wdContext.currentContextElement().getAttch().read(true), new Gaeb());// here attch is the context attribute of type Resource

}catch (Exception ex){

ex.printStackTrace();

}

Regards,

Naga

Former Member
0 Kudos

Hi Naga,

thank you for the quick reply!

I try it with the inputstream, but unfortunately i get not a value back. The method: getValue() gives me "null".

MY XML-File:



<?xml version="1.0" encoding="UTF-8"?>
<!-- Header Part -->
<quoteDoc>
  <head>
    <supplier>Facilita</supplier>
    <quoteNumber>1234567890</quoteNumber>
  </head>
  <positions>
    <item>
      <activityNumber>77.003.04.02</activityNumber>
      <amount>2</amount>
      <unit>ST</unit>
      <room>Z1</room>
    </item>
  </positions>
</quoteDoc>

I never use a xml parser before, so i am not sure if the source code is correct, can you see any mistake in the parser class?

regards,

Sid

Former Member
0 Kudos

Hi Naga,

thanks a lot! There was a mistake in my XML-Class.

Now i solve the problem.

regards,

Sid

Answers (1)

Answers (1)

former_member185086
Active Contributor
0 Kudos

Hi

Please refer this [Blogs|http://www.sdn.sap.com/irj/scn/weblogs;jsessionid=(J2EE3417200)ID0014329650DB11293960846431158245End?blog=/pub/wlg/12082]

Best Regards

Satish Kumar