What is this all about…
In a recent project of mine, I had a requirement of loading a XML file from the client and parsing that file. Obviously I went for FileUpload UI control, but had a few problems figuring out how exactly it works in NW04s (that’s the NW version I am using).
The basic confusion was regarding the “data” attribute of the FileUpload UI control. It’s very vague whether that should be used or not. As it turned out, it doesn’t matter even though it’s flagged as a mandatory field.
So what I’ll try to do in this post is tell you what I had done to achieve this.
Here’s the scenario: I will upload the XML file from the client into the server using FileUpload UI control and parse it using DOM methods. The exact parsing will vary across requirements, so I’ll just create a DOM out of the XML.
The basic steps…
- The view context looks like this:
- FileResource (value attribute, type:com.sap.ide.webdynpro.uielementdefinitions.Resource)
- FileName (value attribute, type:string)
- Create a FileUpload UI control on the view layout.
- Bind the “data” property to the context’s “FileResource” value attribute.
- Bind the “resource” property to the context’s “FileResource” value attribute.
- Create a button on the view, say “Load”.
- Create an action, say “Load” and bind it to the button’s “onAction” event.
- Create a public method called “parseXml” in the component controller.
- The signature looks like:
- public void parseXml(InputStream fileStream)
- The user selects the .xml file by using the FileUpload control.
- The user then clicks on the “Load” button.
The code for onActionLoad method…
The code for the method parseXml…
The necessary imports for using the XML parsing APIs…
The Document interface from org.w3c.dom package has a parse method which takes an java.lang.io.InputStream as a parameter. This is exactly what is needed.
In NW04s, the “resource” property of the FileUpload UI control holds all the data of the file that is being uploaded, not the “data” property. In order to read the file data we will need to use the “IWDResource.read(boolean readMultiple)” method. This method returns an InputStream.
So all we need to do is pass this “InputStream” to the “Document” interface’s “parse” method.
Once we have the DOM we can use it to access the nodes or the like. For example, in the code above we can do something like,