자바로 XPath에 사용하는 방법
- 2010년 4월 28일
- 댓글 쓰기
사용해야 할 XPath에를 Java에서?
다음은 간단한 예제는 XHTML 파일에서 뭔가를 얻으려면입니다
; javax.xml.xpath를 가져 오 *.; ; org.w3c.dom를 가져 오 *.; ; org.w3c.tidy.Tidy을 가져옵니다; ; java.io.File을 가져옵니다; ; java.io.FileInputStream을 가져옵니다; new File ( "/abs/path/to/file" ) ; 파일 파일 = 새로운 파일 ( "/ ABS / 경로 /로 / 파일"); new FileInputStream ( file ) ; FileInputStream XHTML = 새로운 FileInputStream (파일); Tidy ( ) ; 깔끔한 깔끔한) (정리할 = 새; true ) ; . 깔끔한 setQuiet (TRUE); false ) ; . 깔끔한 setShowWarnings (허위); tidy. parseDOM ( xhtml, null ) ; . 문서 박사님 = 깔끔한 parseDOM (XHTML, NULL); ( ) ; . XPathFactory xpf = XPathFactory newInstance (); ( ) ; XPath는 XPath는 = xpf newXPath ().; NodeList ) xpath. evaluate ( "XPath Expr" , doc, NodeList의 노드 = (NodeList) XPath에 있습니다. ( "XPath는 Expr", 의사, 평가 ; XPathConstants NODESET).; / / NodeList의 노드) (현재 nodes.getLength 노드, 예를 들어 있습니다 . println ( nodes. item ( 0 ) . getNodeValue ) ; 시스템 밖 println (nodes. 항목 (0) getNodeValue.)..; . println ( nodes. item ( 1 ) . getTextContent ( ) ; 시스템 밖 println (nodes. 항목 (1) getTextContent ()...; nodes. item ( 2 ) . getChildNodes ( ) . item ( 0 ) . getNodeValue ( ) ; 문자열 fst_child은 = 노드 항목 (2) getChildNodes () 항목 (0) getNodeValue ()....;
자세한 내용은 한 번 받아 javax.xml.xpath 패키지 javadoc 과에서 NodeList 인터페이스를 .


