JAXEN을 이용하여 XML파싱 |
package xml;
import org.jaxen.XPath;
import org.jaxen.dom.DOMXPath;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.util.List;
public class JaxenMain {
public static void main(String[] args) throws Exception {
String xml = "<ROOT><META>메타정보</META><BOOKS><BOOK><AUTHOR>BONG</AUTHOR><TITLE>BONG'S STORY</TITLE></BOOK><BOOK><AUTHOR>HONG</AUTHOR><TITLE>HONG'S STORY</TITLE></BOOK><BOOK><AUTHOR>KIM</AUTHOR><TITLE>KIM'S STORY</TITLE></BOOK></BOOKS></ROOT>";
XPath xpath = new DOMXPath("/ROOT/META");
List<Element> elements = xpath.selectNodes(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xml))).getDocumentElement());
for (Element element : elements) {
System.out.println(element.getNodeName());
System.out.println(element.getTextContent());
}
xpath = new DOMXPath("/ROOT/BOOKS/BOOK/AUTHOR");
elements = xpath.selectNodes(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xml))).getDocumentElement());
for (Element element : elements) {
System.out.println(element.getNodeName());
System.out.println(element.getTextContent());
}
xpath = new DOMXPath("/ROOT/BOOKS/BOOK/TITLE");
elements = xpath.selectNodes(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xml))).getDocumentElement());
for (Element element : elements) {
System.out.println(element.getNodeName());
System.out.println(element.getTextContent());
}
}
}
[참고]
https://openai.com/blog/chatgpt
'Java' 카테고리의 다른 글
[Java] Mybatis Mapper(xml) 파일에서 테이블명 및 뷰명만 추출 (0) | 2021.02.16 |
---|---|
[Java] HashSet을 이용한 중복제거(hashCode, equals 오버라이드) (0) | 2020.10.18 |