Answer the question
In order to leave comments, you need to log in
How to merge xml by id using Java tools?
Good afternoon!
I'm trying to parse data from an xml file using Java. The problem is that in the xml file id can match and you need to combine the matching id and, accordingly, all internal tags. Web search gives results for merging 2 files, but no ID inside one file... Or maybe it's worth merging data after parsing using objects?
<database>
<table id="2638">
<post_title>bla-bla</post_title>
<drivers>bla-bla</drivers>
<drivers_value>bla-bla</drivers_value>
</table>
<table id="2638">
<post_title>bla-bla</post_title>
<packaging>bla-bla</packaging>
<packaging_value>bla-bla</packaging_value>
</table>
<table id="2638">
<post_title>bla-bla</post_title>
<storage>bla-bla</storage>
<storage_value>bla-bla</storage_value>
</table>
<table id="2638">
<post_title>bla-bla</post_title>
<expdate>bla-bla</expdate>
<expdate_value>bla-bla</expdate_value>
</table>
</database>
public class ParseData {
public static void main(String[] args) {
// парсим данные
parseData();
}
private static void parseData () {
// Название файла
File file = new File("xz.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
NodeList tableList = doc.getElementsByTagName("table");
List<Drug> drugList;drugList = new ArrayList<>();
// Цикл
for(int i = 0; i<tableList.getLength(); i++) {
Node tb = tableList.item(i);
Drug drug = new Drug();
if(tb.getNodeType()==Node.ELEMENT_NODE) {
Element table = (Element) tb;
drug.setID(Integer.valueOf(table.getAttribute("id")));
NodeList dataList = table.getChildNodes();
for (int j = 0; j<dataList.getLength(); j++) {
Node n = dataList.item(j);
if(n.getNodeType()==Node.ELEMENT_NODE) {
Element childElement = (Element) n;
switch (childElement.getNodeName()) {
case "ID": {
drug.setID(Integer.valueOf(childElement.getTextContent()));
} break;
case "post_title": {
drug.setPost_title(childElement.getTextContent());
} break;
case "tradename": {
drug.setTradename(childElement.getTextContent());
} break;
case "inname": {
drug.setInname(childElement.getTextContent());
}
// код обрезан
}
}
} // внутренний цикл for
drugList.add(drug);
}
} // внешний цикл for
drugList.forEach(System.out::println);
System.out.println(drugList.size());
/* Здесь начинается добавление в лист */
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Препараты");
Map<String, Object[]> data = new TreeMap<String, Object[]>();
data.put("1", new Object[] {"ID", "tradename","inname","atx","storage",
"expdate", "pharmcondition", "drivers"," packaging","manufacter",
"dosing", "sideeffect", "special", "pregnantuse","kids",
"oldman", "interaction", "overdose","pharmeffect",
"pharmacokinetics","pharmacodynamics"," indications",
"contraindications","dosageform", "composition", "description", "pharmgroup"});
for (int m=0; m<drugList.size(); m++) {
data.put(String.valueOf(m), new Object[]{drugList.get(m).getID(), drugList.get(m).getTradename_value(),
drugList.get(m).getInname_value(), drugList.get(m).getAtx_value(),
drugList.get(m).getStorage_value(), drugList.get(m).getExpdate_value(),
// код обрезан
}
Set<String> keySet = data.keySet();
int rownum = 0;
for (String key : keySet) {
Row row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellNum = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellNum++);
if (obj instanceof String) {
cell.setCellValue((String) obj);
} else if (obj instanceof Integer) {
cell.setCellValue((Integer) obj);
}
}
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream("outputx.xlsx");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
wb.write(fos);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
/* Здесь заканчивается добавление в лист */
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question