"PHP DOMXpath 클래스"의 두 판 사이의 차이

 
(사용자 3명의 중간 판 11개는 보이지 않습니다)
2번째 줄: 2번째 줄:
;PHP DOMXpath 클래스
;PHP DOMXpath 클래스


==예시==
==예시: XML==
* xml 접근 예시
<syntaxhighlight lang='php' run>
<source lang='php'>
<?php
$xml = '<?xml version="1.0"?>
$xml = '<?xml version="1.0"?>
<moldb>
<moldb>
30번째 줄: 28번째 줄:
echo $element->nodeValue.PHP_EOL;
echo $element->nodeValue.PHP_EOL;
}
}
# ala
</syntaxhighlight>
# lys
</source>


*html 접근 예시
==예시: HTML==
<source lang="php">
<syntaxhighlight lang="php" run>
<?php
$html = '<html>
$html = "
<html>
<head>
<head>
<title>Welcome to zetawiki</title>
<title>Welcome to zetawiki</title>
45번째 줄: 39번째 줄:
<p>My name is John</p>
<p>My name is John</p>
</body>
</body>
</html>";
</html>';


$dom = new DOMDocument;
$dom = new DOMDocument;
55번째 줄: 49번째 줄:
     echo $html_p->item(0)->nodeValue;
     echo $html_p->item(0)->nodeValue;
}
}
?>
</syntaxhighlight>
#My name is John
<syntaxhighlight lang='php' run>
</source>
$html = <<<EOF
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
EOF;
 
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//a");
foreach( $elements as $elements ) {
    echo $elements->nodeValue . "\n";
}
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[PHP DOMDocument 클래스]]
*[[PHP DOMDocument 클래스]]


==참고 자료==
==참고==
*http://php.net/manual/kr/class.domxpath.php
* https://www.php.net/manual/en/class.domxpath.php


[[분류: PHP]]
[[분류: PHP XML]]
[[분류: XML]]
[[분류: XPath]]

2021년 9월 12일 (일) 11:29 기준 최신판

1 개요[ | ]

PHP DOMXpath 클래스

2 예시: XML[ | ]

$xml = '<?xml version="1.0"?>
<moldb>
  <molecule>
    <name>Alanine</name>
    <symbol>ala</symbol>
    <code>A</code>
    <type>hydrophobic</type>
  </molecule>
  <molecule>
    <name>Lysine</name>
    <symbol>lys</symbol>
    <code>K</code>
    <type>charged</type>
  </molecule>
</moldb>';

$doc = new DOMDocument();
$doc->loadXML( $xml );

$xpath = new DOMXpath($doc);
$elements = $xpath->query("/moldb/molecule/symbol");
foreach( $elements as $element ) {
	echo $element->nodeValue.PHP_EOL;
}

3 예시: HTML[ | ]

$html = '<html>
<head>
<title>Welcome to zetawiki</title>
</head>
<body>
<p>My name is John</p>
</body>
</html>';

$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$html_p = $xpath->query("//p");

for ($i = 0; $i < $html_p->length; $i++) {
    echo $html_p->item(0)->nodeValue;
}
$html = <<<EOF
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
EOF;

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//a");
foreach( $elements as $elements ) {
    echo $elements->nodeValue . "\n";
}

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}