以下は私のxml形式です。タグの下にデータを抽出する必要があるためGroup
、このグループタグはタグの下にcommon
あります。この共通タグ内には4つのグループがあり、4つgroup
のデータだけが必要です.しかし、私のxmlには他のグループタグがありますそこにもあるので、データも抽出されます。しかし、共通タグ内にそのグループデータが必要です。
私のxml:
<root>
<Budget>100000.0</Budget>
<Procost>
<Costid>1</Costid>
<Costname>Fixed Cost</Costname>
<Amount>70666.66</Amount>
</Procost>
<Procostcount>1</Procostcount>
<Totalcost>70666.66</Totalcost>
<Common>
<Group>
<Empid>370</Empid>
<Reportid>-1</Reportid>
<Empcost>0</Empcost>
<Isanyreporting>Y</Isanyreporting>
<Reportcount>9</Reportcount>
<Reportname>NA</Reportname>
<Name>Shalini</Name>
<Desigid>0</Desigid>
<Gender>female</Gender>
<Yearexperience>11</Yearexperience>
<Monthexperience>0</Monthexperience>
<Phone>974113524</Phone>
<Emailid>shalini@ohile.com</Emailid>
<Projectid>1</Projectid>
<Projectname>Sabre</Projectname>
<Displaylevel>0</Displaylevel>
<Photo>None</Photo>
</Group>
<Group>
<Empid>381</Empid>
<Reportid>370</Reportid>
<Empcost>0</Empcost>
<Reportcount>0</Reportcount>
<Isanyreporting>No</Isanyreporting>
<Reportname>Shalini</Reportname>
<Name>Arun</Name>
<Desigid>1</Desigid>
<Gender>male</Gender>
<Yearexperience>8</Yearexperience>
<Monthexperience>0</Monthexperience>
<Phone>2147483647</Phone>
<Emailid>arun@ohile.com</Emailid>
<Projectid>1</Projectid>
<Projectname>Sabre</Projectname>
<Displaylevel>0</Displaylevel>
<Photo>None</Photo>
</Group>
<Group>
<Empid>382</Empid>
<Reportid>370</Reportid>
<Empcost>0</Empcost>
<Reportcount>0</Reportcount>
<Isanyreporting>No</Isanyreporting>
<Reportname>Shalini</Reportname>
<Name>Manu</Name>
<Desigid>1</Desigid>
<Gender>male</Gender>
<Yearexperience>8</Yearexperience>
<Monthexperience>0</Monthexperience>
<Phone>123487509</Phone>
<Emailid>manu@ohile.com</Emailid>
<Projectid>1</Projectid>
<Projectname>Sabre</Projectname>
<Displaylevel>0</Displaylevel>
<Photo>None</Photo>
</Group>
<Group>
<Empid>383</Empid>
<Reportid>370</Reportid>
<Empcost>0</Empcost>
<Reportcount>0</Reportcount>
<Isanyreporting>No</Isanyreporting>
<Reportname>Shalini</Reportname>
<Name>Ramakrishnan Nair</Name>
<Desigid>1</Desigid>
<Gender>male</Gender>
<Yearexperience>8</Yearexperience>
<Monthexperience>0</Monthexperience>
<Phone>467983245</Phone>
<Emailid>ramakrishnan@ohile.com</Emailid>
<Projectid>1</Projectid>
<Projectname>Sabre</Projectname>
<Displaylevel>0</Displaylevel>
<Photo>None</Photo>
</Group>
</Common>
<Project>
<Empid>371</Empid>
<Reportid>370</Reportid>
<Displaylevel>2</Displaylevel>
<Empcost>35333.3</Empcost>
<Name>Remya</Name>
<Desigid>5</Desigid>
<Gender>female</Gender>
<Yearexperience>2</Yearexperience>
<Monthexperience>0</Monthexperience>
<Phone>975443452</Phone>
<Emailid>remya@ohile.com</Emailid>
<Projectid>1</Projectid>
<Projectname>Sabre</Projectname>
<Photo>None</Photo>
<Reportcount>0</Reportcount>
<Isanyreporting>No</Isanyreporting>
<Reportname>Shalini</Reportname>
</Project>
<Project>
<Empid>374</Empid>
<Reportid>370</Reportid>
<Displaylevel>2</Displaylevel>
<Empcost>35333.3</Empcost>
<Name>Nijo Susan Thomas</Name>
<Desigid>5</Desigid>
<Gender>female</Gender>
<Yearexperience>1</Yearexperience>
<Monthexperience>1</Monthexperience>
<Phone>2147483647</Phone>
<Emailid>nijo@ohile.com</Emailid>
<Projectid>1</Projectid>
<Projectname>Sabre</Projectname>
<Photo>None</Photo>
<Reportcount>0</Reportcount>
<Isanyreporting>No</Isanyreporting>
<Reportname>Shalini</Reportname>
</Project>
<Count>6</Count>
<Procount>2</Procount>
<Fieldnames>
<Fieldnamecount>16</Fieldnamecount>
<Group>
<Fieldname>Name</Fieldname>
</Group>
<Group>
<Fieldname>Empid</Fieldname>
</Group>
<Group>
<Fieldname>Desigid</Fieldname>
</Group>
<Group>
<Fieldname>Gender</Fieldname>
</Group>
<Group>
<Fieldname>Yearexperience</Fieldname>
</Group>
<Group>
<Fieldname>Monthexperience</Fieldname>
</Group>
<Group>
<Fieldname>Phone</Fieldname>
</Group>
<Group>
<Fieldname>Emailid</Fieldname>
</Group>
<Group>
<Fieldname>Projectid</Fieldname>
</Group>
<Group>
<Fieldname>Reportid</Fieldname>
</Group>
<Group>
<Fieldname>Isanyreporting</Fieldname>
</Group>
<Group>
<Fieldname>Reportcount</Fieldname>
</Group>
<Group>
<Fieldname>Reportname</Fieldname>
</Group>
<Group>
<Fieldname>Photo</Fieldname>
</Group>
<Group>
<Fieldname>Displaylevel</Fieldname>
</Group>
<Group>
<Fieldname>Empcost</Fieldname>
</Group>
</Fieldnames>
</root>
私のコード:
public class XMLParsingDomExample extends Activity {
List myEmpls;
Document dom;
public XMLParsingDomExample() {
myEmpls = new ArrayList();
parseXmlFile();
parseDocument();
printData();
}
/*public void runExample() {
}*/
private void parseXmlFile() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
System.out.println("Inside xml");
try {
URL url = new URL(
"http://fhkghkkl.guio");
// "http://totheriver.com/learn/xml/code/employees.xml");
DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse(new InputSource(url.openStream()));
dom.getDocumentElement().normalize();
System.out.println("after xml");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (SAXException se) {
se.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private void parseDocument() {
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getElementsByTagName("Group");
// NodeList nl = docEle.getElementsByTagName("Common");
// NodeList nl = dom.getDocumentElement().getChildNodes();
System.out.println("nodelist is :" + nl);
if (nl != null && nl.getLength() > 0) {
for (int i = 0; i < nl.getLength(); i++) {
Element el = (Element) nl.item(i);
System.out.println("b4 getemployee");
Employee e = getEmployee(el);
myEmpls.add(e);
System.out.println("employee size is =" + myEmpls.size());
}
}
}
private Employee getEmployee(Element empE1) {
String empId = getTextValue(empE1, "Empid");
String name = getTextValue(empE1, "Name");
String repCount = getTextValue(empE1, "Reportcount");
String desid = getTextValue(empE1, "Desigid");
String projectid = getTextValue(empE1, "Projectid");
String projectname = getTextValue(empE1, "Projectname");
// String gender =
// getTextValue(empE1, "Gender");
// int yrEx = getIntValue(empE1,
// "Yearexperience");
// int mnthEx = getIntValue(empE1, "Monthexperience");
// int prcId = getIntValue(empE1, "ProjectId"); String prcname =
// getTextValue(empE1,
// "Projectname");
// int dislev = getIntValue(empE1, "Displaylevel"); String photo
// = getTextValue(empE1, "Photo");
// int phone = getIntValue(empE1, "Phone");
// String emailid = getTextValue(empE1, "Emailid");
Employee e = new Employee(empId, name, repCount, desid, projectid,
projectname);
return e;
}
// }
private String getTextValue(Element ele, String tagName) {
String textVal = null;
NodeList nl = ele.getElementsByTagName(tagName);
if (nl != null && nl.getLength() > 0) {
Element el = (Element) nl.item(0);
textVal = el.getFirstChild().getNodeValue();
}
return textVal;
}
private int getIntValue(Element ele, String tagName) {
// in production application you would catch the exception
return Integer.parseInt(getTextValue(ele, tagName));
}
private void printData() {
System.out.println("No of Employees '" + myEmpls.size() + "'.");
Iterator it = myEmpls.iterator();
while (it.hasNext()) {
System.out.println(it.next().toString());
}
}
}
/*public static void main(String[] args) {
XMLParsingDomExample dpe = new XMLParsingDomExample();
dpe.runExample();
}
}*/
私のlogcat:
私はこれだけが必要です:
01-22 13:10:44.519: I/System.out(1606): Employee Details - EmployeeId:370.Empname :Shalini, DesignId:9, ReportCount:0, ProjectId:1, ProjectName:Sabre,
01-22 13:10:44.537: I/System.out(1606): Employee Details - EmployeeId:381.Empname :Arun, DesignId:0, ReportCount:1, ProjectId:1, ProjectName:Sabre,
01-22 13:10:44.537: I/System.out(1606): Employee Details - EmployeeId:382.Empname :Manu, DesignId:0, ReportCount:1, ProjectId:1, ProjectName:Sabre,
01-22 13:10:44.547: I/System.out(1606): Employee Details - EmployeeId:383.Empname :Ramakrishnan Nair, DesignId:0, ReportCount:1, ProjectId:1, ProjectName:Sabre,
01-22 13:10:44.547: I/System.out(1606):
しかし、これも来ています:
Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,
01-22 13:10:44.578: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,
01-22 13:10:44.578: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,
01-22 13:10:44.597: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,
01-22 13:10:44.597: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,
01-22 13:10:44.617: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,
01-22 13:10:44.617: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,
01-22 13:10:44.637: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,
01-22 13:10:44.637: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,
01-22 13:10:44.657: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,
01-22 13:10:44.657: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,
01-22 13:10:44.667: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,
01-22 13:10:44.677: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,
01-22 13:10:44.677: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,
01-22 13:10:44.687: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,
01-22 13:10:44.698: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,