aspose file tools*
The moose likes XML and Related Technologies and the fly likes Unable to get the correct data from the XML file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Unable to get the correct data from the XML file" Watch "Unable to get the correct data from the XML file" New topic
Author

Unable to get the correct data from the XML file

Chinni Bujji
Ranch Hand

Joined: Jan 31, 2007
Posts: 60
/*
* Main.java
*
* Created on February 6, 2007, 9:56 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package javaapplication5;

import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;


/**
*
* @author isultana
*/
public class Main {

/** Creates a new instance of Main */
public Main() {
}

public static void main (String argv []){
try {

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("sample.xml"));

// normalize text representation doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());


NodeList listOfRoles = doc.getElementsByTagName("Role");
int totalRoles = listOfRoles.getLength();
System.out.println("Total no of Roles : " + totalRoles);

for(int s=0; s<listOfRoles.getLength() ; s++){


Node firstPersonNode = listOfRoles.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){


Element firstPersonElement = (Element)firstPersonNode;

//-------
NodeList firstNameList = firstPersonElement.getElementsByTagName("RoleName");
Element firstNameElement = (Element)firstNameList.item(0);

NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("Role Name : " +
((Node)textFNList.item(0)).getNodeValue().trim());

//-------
NodeList lastNameList = firstPersonElement.getElementsByTagName("Menu");
int totalMenus = lastNameList.getLength();
System.out.println("Total no of Menus : " + totalMenus);



Element lastNameElement = (Element)lastNameList.item(0);

NodeList textLNList = lastNameElement.getChildNodes();
System.out.println(textLNList.getLength());

System.out.println("Menu : " +
((Node)textLNList.item(0)).getNodeValue().trim());

lastNameElement = (Element)lastNameList.item(1);

//textLNList = lastNameElement.getChildNodes();


System.out.println("Menu : " +
((Node)textLNList.item(0)).getNodeValue().trim());

//----
//NodeList ageList = firstPersonElement.getElementsByTagName("age");
//Element ageElement = (Element)ageList.item(0);

//NodeList textAgeList = ageElement.getChildNodes();
//System.out.println("Age : " +
//((Node)textAgeList.item(0)).getNodeValue().trim());

//------

}//end of if clause

}//end of for loop with s var

}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line "
+ err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());

}catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();

}catch (Throwable t) {
t.printStackTrace ();
}
//System.exit (0);
}//end of main

}


my xml file
<RoleList>
<Role>
<RoleName>Manager</RoleName>
<Menu>WorkQueue</Menu>
<Menu>Admin Tools</Menu>
</Role>
<Role>
<RoleName>Clerk</RoleName>
<Menu>WorkQueue</Menu>
</Role>
</RoleList>

I want to print the rolename and menus
but cannot only one menu is printed not the two in the first node
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Unable to get the correct data from the XML file