on 01-22-2009 8:36 AM
Hi,
In my scenirio i have to facth multiple values from XML file and then set in to Table in webdynpro...
Fo example my xml has values like...
<xml>
<item>
<item1>
<quantity>
100
</quantity>
<price>
50
</price>
</item1>
<item2>
<quantity>
200
</quantity>
<price>
20
</price>
</item2>
<item3>
<quantity>
300
</quantity>
<price>
10
</price>
</item3>
</item>
</xml>
then i have to fcath those quantity and price and set in to table...
How to do taht in webdynpro and does any one have parser code for retriving multiple values...
Hi,
1) You need to use JDOM parser.
2) The code for parsing XML using JDOM parser is readily available if you search on google.
3) You will have to check the attribute during every parsing and then if attribute is quantity you can fetch the corresponding tags.
4) Something similar to this:
org.jdom.Document document = parser.build(file);
org.jdom.Element rootElement = document.getRootElement();
org.jdom.Element childElement = rootElement.getChild("file");
Element xmlElement = childElement.getChild("item");
if (xmlElement != null) {
List itemElementsList = xmlElement.getChildren("item1");
if (itemElementsList != null) {
Iterator iterator3 = itemElementsList.iterator();
while (iterator3.hasNext()) {
//For each group get quantity
Element itemElement = (Element) iterator3.next();
List quantityElementsList =
itemElement.getChildren("quantity");
if (quantityElementsList != null) {
Iterator iterator2 =
quantityElementsList.iterator();
while (iterator2.hasNext()) {
// Your code
}
}
}
}
}
You might need to make some changes as per your rquirement. Just use this sample to understand how you need to parse the xml
Hope it helps.
Regards.
Rajat
Edited by: Rajat Jain on Jan 22, 2009 9:51 AM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
solved
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
You need to specify the path as given below.
File file = new File("d://sample.xml");
In jave for using / this you need to put it twice(which is called escape character)
Hope this will help
Regards
Narendra
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Narendra,
I tried for local machine option, but when I am specifying the path like :-
File file = new File("d:\sample.xml");
It is giving error saying invalid escape sequence. Can you spcifically tell me how to give the file path?
Thanks,
Qutubuddin Saifee
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
The code i have given will work for local file which is stored in your machine.
If your file is in KM then you need to use the file download elements to fetch the file and pass it to the code, then only it will work.
Else you need to save the xml file from server to your machine and pasrse it using the above code.
Hope this will help.
Regards
Narendra
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
To upload the file from KM, check the following link:
/people/rohit.radhakrishnan/blog/2005/05/27/uploading-files-to-km-repository-using-webdynpro-apis
It may help.
Regards.
Rajat
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Refer these links,
http://help.sap.com/saphelp_nw04/helpdata/en/d2/369f1dddb7ff4c96c2bacdd7aa6c87/frameset.htm
Regards,
Sunaina Reddy T
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Thanks for your quick responses.
I checked with the code sent by Narendra Singh.
When I am giving full path name like below:-
File file = new File("http://epserver:50000/irj/go/km/docs/documents/Q/sample.xml");
I am not getting the file. Is this not the correct way to specify the file path?
My xml file is in KM.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Below is the code to read sample xml as given by you.
In this i have written System.out.println to print the values of key, name,id but you can directly assign the values to the node element.
i hope this will help . please revert if you require more info
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XMLReader {
public static void main(String argv[]) {
try {
File file = new File("d:
data.xml");//pass the full path of xml file
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("item");
for (int s = 0; s < nodeLst.getLength(); s++) {
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("quantity");
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
System.out.println("quantity: " + ((Node) fstNm.item(0)).getNodeValue());
NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("price");
Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
NodeList lstNm = lstNmElmnt.getChildNodes();
System.out.println("price: " + ((Node) lstNm.item(0)).getNodeValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Hope this will help you.
please revert if you require more info.
Regards
Narendra
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
101 | |
13 | |
13 | |
11 | |
11 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.