Topic
1 reply Latest Post - ‏2012-04-18T17:20:25Z by JAntley
SystemAdmin
SystemAdmin
770 Posts
ACCEPTED ANSWER

Pinned topic reading child tags in xml file

‏2012-04-12T11:15:44Z |
Hi,
I have tried to read the xml file.But here i am using getElementsByTag("Attribute").it will take 6 tags.i need to take first 2 only.I am writing the code like this. can u check and update me.
public ArrayList<AppDescriptorBean> getAppDescriptorListFromXML(Document domFetch) throws Exception {
ArrayList<AppDescriptorBean> listBean = new ArrayList<AppDescriptorBean>();

try {
Element docEle = domFetch.getDocumentElement();
System.out.println("getAssetListFromXMLRelation ***** ");
NodeList serverAssetes = docEle.getElementsByTagName("Sever-assets");
if (serverAssetes != null && serverAssetes.getLength() > 0) {
for (int j = 0; j < serverAssetes.getLength(); j++) {
Element sael = (Element) serverAssetes.item(j);

String community = getTextFromXML(sael, "community");
String assetType = getTextFromXML(sael, "assetType");

System.out.println(" getAssetListFromXMLRelation community : "+community);
System.out.println(" getAssetListFromXMLRelation assetType : "+assetType);

NodeList nl = docEle.getElementsByTagName("Server-asset");
if (nl != null && nl.getLength() > 0) {
for (int i = 0; i < nl.getLength(); i++) {
System.out.println(" getAssetListFromXMLRelation loop..... ");
try {
Element el = (Element) nl.item(i);

String assetName = getTextFromXML(el, "assetname");
String version = getTextFromXML(el, "version");
System.out.println(" getAssetListFromXMLRelation assetName : "+assetName);

NodeList fetchAppList = el.getElementsByTagName("Base-descriptor");
if (fetchAppList != null && fetchAppList.getLength() > 0) {
for (int k = 0; k < fetchAppList.getLength(); k++) {
AppDescriptorBean appDescriptorBean = new AppDescriptorBean();
HashMap assetAttrMap = new HashMap();
Element appel = (Element) fetchAppList.item(k);
System.out.println("base descriptor loop");
NodeList fetchAttrList = appel.getElementsByTagName("Attribute");
if (fetchAttrList != null && fetchAttrList.getLength() > 0) {
for (int x = 0; x < fetchAttrList.getLength(); x++) {
Element attrel = (Element) fetchAttrList.item(x);

String attributeName = getTextFromXML(attrel, "name");
String attributeValue = getTextFromXML(attrel, "value");

System.out.println(" getAssetListFromXMLRelation attributeName : "+attributeName);
System.out.println(" getAssetListFromXMLRelation attributeValue : "+attributeValue);
assetAttrMap.put(attributeName, attributeValue);
}
}
NodeList fetchChildAppList = appel.getElementsByTagName("Component-decriptors");
if(fetchChildAppList != null && fetchChildAppList.getLength()>0){

for(int l=0;l<fetchChildAppList.getLength();l++){
System.out.println("Component-decriptors loop----");
NodeList fetchComponentList = appel.getElementsByTagName("Component");
if(fetchComponentList != null && fetchComponentList.getLength()>0){
for(int m=0;m<fetchChildAppList.getLength();m++){
System.out.println("Component loop----");

NodeList fetchAttrList1 = appel.getElementsByTagName("Attribute");
if (fetchAttrList1 != null && fetchAttrList1.getLength() > 0) {
for (int x = 0; x < fetchAttrList1.getLength(); x++) {
Element attrel = (Element) fetchAttrList1.item(x);

String attributeName = getTextFromXML(attrel, "name");
String attributeValue = getTextFromXML(attrel, "value");
System.out.println(" getAssetListFromXMLRelation in attribute component");
System.out.println(" getAssetListFromXMLRelation attributeName : "+attributeName);
System.out.println(" getAssetListFromXMLRelation attributeValue : "+attributeValue);
assetAttrMap.put(attributeName, attributeValue);
}
}
}
}
}
assetAttrMap.put("HostName", assetName);
System.out.println(" getAssetListFromXMLRelation assetName : "+assetName);
appDescriptorBean.setRamCommunity(community);
appDescriptorBean.setRamAssetType(assetType);
appDescriptorBean.setRamAssetName(assetName);
appDescriptorBean.setHostName(assetName);
appDescriptorBean.setRamAssetVersion(version);
appDescriptorBean.setRamAssetAtributes(assetAttrMap);
listBean.add(k,appDescriptorBean);
}
}

}
}catch (Exception e) {
// log.error(e);
// assetBean.setErrorMessage(e.getMessage());
//assetBean.setStatus("Error");
//listBean.add(appDescriptorBean);
}
}
//log.info(listBean.size() + " size ");

}
}
}
} catch (Exception e) {
//log.error(e);
throw new Exception("Invalid XML file");
}
return listBean;
}
Updated on 2012-04-18T17:20:25Z at 2012-04-18T17:20:25Z by JAntley
  • JAntley
    JAntley
    109 Posts
    ACCEPTED ANSWER

    Re: reading child tags in xml file

    ‏2012-04-18T17:20:25Z  in response to SystemAdmin
    Hello ngadipudi,

    The forum where you have posted your questions is primarily for those who have questions about using the developerWorks site itself. You would have a better chance of getting the answers you want if you were to post in one of the forums for XML or for the programming language you are using. Here are the XML forums: http://www.ibm.com/developerworks/forums/dw_xforums.jspa

    Let us know if we can be of additional assistance.

    Regards,
    Jeff Antley
    developerWorks Feedback Team