cancel
Showing results for 
Search instead for 
Did you mean: 

Java Mapping CLarification

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

Its because of for loop condition, use item<=child.getLength();

use <= you will get.

Thanks,

RamuV

Former Member
0 Kudos

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

Former Member
0 Kudos