on 07-11-2008 2:51 PM
hi guys,
am new to the java mapping trying out to explore the dom type
Source XML:
<?xml version="1.0" encoding="UTF-8"?>
<Orders type="srinivas">
<Header>
<OrderId test="test">123456</OrderId>
<OrderTotal>57.65</OrderTotal>
<ItemId>30</ItemId>
<Description>Something Else</Description>
<Quantity>2</Quantity>
<UnitPrice>3.95</UnitPrice>
</Header>
</Orders>
Target XML:
<?xml version="1.0" encoding="UTF-8"?>
<Orders type="srinivas">
<Header>
<OrderId test="test">123456</OrderId>
</Header>
<OrderTotal>57.65</OrderTotal>
<Header>
<ItemId>30</ItemId>
</Header>
<Header>
<Description>Something Else</Description>
</Header>
<Quantity>2</Quantity>
<Header>
<UnitPrice>3.95</UnitPrice>
</Header>
</Orders>
Now what i did was as below:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db =dbf.newDocumentBuilder();
Document doc = db.parse(in);
Node orders = doc.getFirstChild();
NodeList child = orders.getChildNodes();
for (int item = 0; item<child.getLength();item++)
{
Node node = child.item(item);
System.out.println("count""::"item);
System.out.println("node"+node.toString());
}
for this i donot understand here for item 0 : nothing comes out for item 1 it gives xml starting with <header> completely with other elements inside.
now having this further how can i get each element , can any one help me out in the code above , and list some important methods and it functions.
please i do have enough docs , try to reply if possible preceisly to my questions
regds
srini
Hi,
Its because of for loop condition, use item<=child.getLength();
use <= you will get.
Thanks,
RamuV
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Venkat,
The code you have written is returning corret result only. The Item 0 under header returns a text node which is nothing but the value inside Header node which is blank in your case. Item 1 is the node element which is orders, so your code is behaving correctly.
In order to get all the line items inside Orders, you need to have a recursive loop. the code for which is shown as below. Pls modify this code based on your exact requirement and it will work for sure. this is just sample code I have written to help you out.
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db =dbf.newDocumentBuilder();
Document doc = db.parse(in);
traverse(doc);
}
private void traverse(Document doc)
{
NodeList orders = doc.getChildNodes();
for(int i=0;i<orders.getLength();i++)
{
String lgstr=orders.item(i).getNodeName();
traverse(orders.item(i));
}
}
public void traverse(Node node)
{
int type = node.getNodeType();
if (type == Node.ELEMENT_NODE)
System.out.println ("Name :"+node.getNodeName());
if(!node.hasChildNodes())
System.out.println ("Value :"+node.getNodeValue());
NodeList children = node.getChildNodes();
if (children != null) {
for (int i=0; i< children.getLength(); i++)
traverse (children.item(i));
}
}
thanks
amit
reward points if answer is useful
HI
Refer the following blogs:
http://help.sap.com/saphelp_nw04/helpdata/en/e2/e13fcd80fe47768df001a558ed10b6/frameset.htm
Testing and debugging
Implermenting JAVA Mapping in PI
Message mapping internally generates DOM parser.
Java Mapping in XI
SAP Network Blog: Implementing a Java Mapping in SAP PI
Java Mapping (SAP Library - Partner Connectivity Kit)
http://help.sap.com/saphelp_nw04/helpdata/en/e2/e13fcd80fe47768df001a558ed10b6/frameset.htm
SAP Network Blog: XI Java Mapping Helper (DOM)
SAP Network Blog: Testing and Debugging Java Mapping
Binary Conversion in XI - Java Mapping - Code Gallery - Wiki
http://help.sap.com/saphelp_nw04/helpdata/en/e2/e13fcd80fe47768df001a558ed10b6/content.htm
http://help.sap.com/saphelp_nw04s/helpdata/en/43/09b16006526e72e10000000a422035/frameset.htm
https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/5242. [original link is broken] [original link is broken] [original link is broken]
cheers
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.