Далее я хотел бы упорядочить узел <table>..</table>
в соответствии со входом (inputY
, inputX
в этом случае) в атрибуте id
или удалить <table>..</table>
, если отправляется только один вход. Как я могу добиться этого с помощью парсера DOM.
<employees>
<table>
<employee>
<id attr="inputY">
<firstName>Lokesh</firstName>
<lastName>Gupta</lastName>
<department>
<id>101</id>
<name>IT</name>
</department>
</id>
</employee>
</table>
<table>
<employee>
<id attr="inputX">
<firstName>Brian</firstName>
<lastName>Schultz</lastName>
<department>
<id>102</id>
<name>HR</name>
</department>
</id>
</employee>
</table>
<employees>
Если ввод передается в порядке inputX
и inputY
, тогда XML будет выглядеть следующим образом:
<employees>
<table>
<employee>
<id attr="inputX">
<firstName>Brian</firstName>
<lastName>Schultz</lastName>
<department>
<id>102</id>
<name>HR</name>
</department>
</id>
</employee>
</table>
<table>
<employee>
<id attr="inputY">
<firstName>Lokesh</firstName>
<lastName>Gupta</lastName>
<department>
<id>101</id>
<name>IT</name>
</department>
</id>
</employee>
</table>
<employees>
Это то, что я сделал пока:
public static void main(String... args) throws Exception {
DocumentBuilder db = DocumentBuilderFactory.newInstance().
newDocumentBuilder();
Document doc = db.parse("src/main/resources/some1.xml");
doc.getDocumentElement().normalize();
ArrayList<String> ids = new ArrayList<String>();
ids.add("inputY");
ids.add("inputX");
Element root = doc.getDocumentElement();
Node employees = root.getElementsByTagName("employees").item(0);
NodeList moveList = doc.getElementsByTagName("table");
for (int k = 0; k < moveList.getLength(); k++) {
System.out.println(moveList.item(k));
Node move = moveList.item(k);
Element eMove = (Element) move;
NodeList idList = eMove.getElementsByTagName("id");
for (int i = 0; i < idList.getLength(); i++) {
if (i < ids.size()) {
boolean result = ids.contains(
idList.item(0).getAttributes().item(0).
getNodeValue());
if (result) {
//System.out.println("parent node : " + move.getParentNode().getFirstChild());
Node currentFirstNode = employees.getFirstChild();
Node copyNode = move.cloneNode(true);
Node placeholder = currentFirstNode.getParentNode();
placeholder.insertBefore(copyNode,currentFirstNode);
placeholder.removeChild(move);
}
}
}
}
}
Обновление 2:
Вот мой новый код: Тем не менее он не может правильно упорядочить узлы. узел с атрибутом inputX находится перед inputZ, хотя у меня есть inputZ перед входом X в списке. Любое предложение?
DocumentBuilder db = DocumentBuilderFactory.newInstance().
newDocumentBuilder();
Document doc = db.parse("src/main/resources/some1.xml");
doc.getDocumentElement().normalize();
ArrayList<String> ids = new ArrayList<String>();
ids.add("inputZ");
ids.add("inputX");
Element root = doc.getDocumentElement();
Node employees = root.getElementsByTagName("employees").item(0);
NodeList moveList = doc.getElementsByTagName("table");
for (int k = 0; k < moveList.getLength(); k++) {
System.out.println("# of table nodes : " + moveList.getLength());
Node move = moveList.item(k);
Element eMove = (Element) move;
NodeList idList = eMove.getElementsByTagName("id");
System.out.println("id attribute : " + idList.item(0).getAttributes().item(0));
boolean result = ids.contains(
idList.item(0).getAttributes().item(0).
getNodeValue());
if (result) {
System.out.println(result);
Node currentFirstNode = employees.getFirstChild();
Node placeholder = currentFirstNode.getParentNode();
placeholder.insertBefore(move, currentFirstNode);
} else {
System.out.println(result);
System.out.println("Employees child node : " + employees.getChildNodes());
employees.removeChild(move);
}
}
XML:
<root>
<employees>
<table>
<employee>
<id attr="inputZ">
<firstName>Ben</firstName>
<lastName>Smith</lastName>
<department>
<id>103</id>
<name>Business</name>
</department>
</id>
</employee>
</table>
<table>
<employee>
<id attr="inputX">
<firstName>Brian</firstName>
<lastName>Schultz</lastName>
<department>
<id>102</id>
<name>HR</name>
</department>
</id>
</employee>
</table>
<table>
<employee>
<id attr="inputY">
<firstName>Lokesh</firstName>
<lastName>Gupta</lastName>
<department>
<id>101</id>
<name>IT</name>
</department>
</id>
</employee>
</table>
</employees>
</root>