반응형
자바 : org.w3c.dom.document에서 xpath 문자열을 통해 요소를 찾는 방법
주어진 org.w3c.dom.document에서 xpath 문자열을 통해 요소 / 요소를 어떻게 빠르게 찾습니까? FindElementsByXpath()
방법 이없는 것 같습니다 . 예를 들면
/html/body/p/div[3]/a
동일한 이름의 요소가 많을 때 모든 자식 노드 수준을 반복적으로 반복하는 것이 매우 느리다는 것을 알았습니다. 어떤 제안?
파서 나 라이브러리를 사용할 수 없으며 w3c dom 문서로만 작업해야합니다.
이 시도:
//obtain Document somehow, doesn't matter how
DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document doc = b.parse(new FileInputStream("page.html"));
//Evaluate XPath against Document itself
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList)xPath.evaluate("/html/body/p/div[3]/a",
doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); ++i) {
Element e = (Element) nodes.item(i);
}
다음 page.html
파일로 :
<html>
<head>
</head>
<body>
<p>
<div></div>
<div></div>
<div><a>link</a></div>
</p>
</body>
</html>
반응형
'IT TIP' 카테고리의 다른 글
Java의 HttpServletRequest에서 원시 게시물 데이터를 검색하는 방법 (0) | 2021.01.07 |
---|---|
'&'대 '&&'사용 (0) | 2021.01.07 |
isset () 대 strlen ()-빠르고 명확한 문자열 길이 계산 (0) | 2021.01.07 |
SQL Server Express (2012)와 LocalDB간에 차이가 있습니까? (0) | 2021.01.07 |
PANDAS는 여러 Y 축을 플롯합니다. (0) | 2021.01.07 |