이 XML을 정렬하려고합니다.
<phoneBook first="JJ">
<last>Jo</last>
<type>Cellphone</type>
</phoneBook>
마지막 요소로.
public static Map<String, Node> sortElements(Document doc, Map<String, Node> map, String sortBy){
if(map == null)
map = new HashMap<>();
NodeList selection =doc.getElementsByTagName("phoneBook");
for(int i = 0; i < selection.getLength(); i++){
Node rel = selection.item(i);
for(int j = 0; j < rel.getChildNodes().getLength(); j++){
Node jn = rel.getChildNodes().item(j);
if(jn.getNodeName().equals(sortBy)){
map.put(jn.getNodeValue(), rel);
}}}
return new TreeMap<>(map);
}
불행히도 하나의 노드가 비어 있습니다. DOM Parser에서 이와 같은 메서드를 호출합니다!
SortPhoneBook.sortElements(doc, null, "last");
아무도 나를 도울 수 있습니까?
맵 키에 null 값을 넣습니다.
코드 :
map.put(jn.getNodeValue(), rel);
다음으로 교체하십시오.
map.put(jn.getTextContent(), rel);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다