xml 파일이 있고 제목과 첫 번째 작성자를 얻으려고합니다. java xml 하위 노드에서 속성 값을 가져 오는 중
이
는 데이터 메신저와 협력 :<citation type="Book" date="1986" name="Book name">
<title>
a fun name for a book
</title>
<authorList>
<person name="Person 1"/>
<person name="Person 2"/>
<person name="Person 3"/>
</authorList>
</citation>
<citation type="Book" date="1986" name="Another book">
<title>
a boring book title
</title>
<authorList>
<person name="Person A"/>
<person name="Person B"/>
<person name="Person C"/>
</authorList>
</citation>
내가
NodeList itemsCitation = doc.getElementsByTagName("citation");
for (int i = 0; i < itemsCitation.getLength(); i++) {
Node n = itemsCitation.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) n;
NodeList titleNode = e.getChildNodes();
for (int j = 0; j < titleNode.getLength(); j++) {
Node n2 = titleNode.item(j);
if (n2.getNodeType() == Node.ELEMENT_NODE && n2.getNodeName().equals("title")) {
System.out.println(n2.getTextContent());
}
if (n2.getNodeType() == Node.ELEMENT_NODE && n2.getNodeName().equals("authorList")) {
// code i dont have
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
을 쓴 코드
임려고 출력은 다음과 같습니다
a fun name for a book
Person 1
a boring book title
Person A
제목의 ISN '를 얻기 문제는 아니지만 첫 번째 저자를 얻는 것이 중요합니다. TextValent를 얻으려고하면 NodeValue를 얻으려고하면 "null"이됩니다. 빈 행만 얻습니다. 누군가가 나를 도울 수 있기를 바랍니다. 미리 감사드립니다!
아, 내 JAXB를 사용하면 간단하지 않을까요? – laune