라이브러리

[PHP] XMLReader::getAttributeNo - 인덱스로 속성 값을 가져옵니다.




XMLReader::getAttribute()


XMLReader는 PHP의 내장 라이브러리 중 하나로, XML 문서를 읽고 처리하는 데 사용됩니다. XMLReader::getAttribute() 메서드는 XMLReader 객체에 연결된 현재 노드의 특성을 반환합니다.

# 사용법


XMLReader::getAttribute() 메서드는 두 개의 매개변수를 받습니다.

* `$name`: 특성 이름
* `$namespace`: 특성 이름스페이스 (선택 사항)

# 예제


다음 예제는 XMLReader::getAttribute() 메서드를 사용하여 XML 문서의 특성을 읽는 방법을 보여줍니다.

#hostingforum.kr
php

// XML 문서

$xml = 'John30';



// XMLReader 객체 생성

$xmlReader = new XMLReader();

$xmlReader->xml($xml);



// 현재 노드의 특성 읽기

while ($xmlReader->read()) {

    if ($xmlReader->nodeType == XMLReader::ELEMENT) {

        $name = $xmlReader->name;

        $attribute = $xmlReader->getAttribute('name');

        echo "Element: $name, Attribute: $attribute
";

    }

}



// XMLReader 객체 닫기

$xmlReader->close();



# 결과


#hostingforum.kr


Element: name, Attribute: John



# 예제 2: 특성 이름스페이스 사용


다음 예제는 XMLReader::getAttribute() 메서드를 사용하여 XML 문서의 특성을 읽는 방법을 보여줍니다. 이 예제에서는 특성 이름스페이스를 사용합니다.

#hostingforum.kr
php

// XML 문서

$xml = 'John30';



// XMLReader 객체 생성

$xmlReader = new XMLReader();

$xmlReader->xml($xml);



// 현재 노드의 특성 읽기

while ($xmlReader->read()) {

    if ($xmlReader->nodeType == XMLReader::ELEMENT) {

        $name = $xmlReader->name;

        $namespace = $xmlReader->namespaceURI;

        $attribute = $xmlReader->getAttributeNS($namespace, 'name');

        echo "Element: $name, Namespace: $namespace, Attribute: $attribute
";

    }

}



// XMLReader 객체 닫기

$xmlReader->close();



# 결과


#hostingforum.kr


Element: ns:name, Namespace: http://example.com/ns, Attribute: John



# 참고


XMLReader::getAttribute() 메서드는 XMLReader 객체에 연결된 현재 노드의 특성을 반환합니다. 이 메서드는 두 개의 매개변수를 받습니다. `$name`은 특성 이름이며, `$namespace`은 특성 이름스페이스입니다. 이 예제에서는 두 개의 예제를 보여줍니다. 첫 번째 예제에서는 특성 이름스페이스를 사용하지 않은 경우를 보여주고, 두 번째 예제에서는 특성 이름스페이스를 사용한 경우를 보여줍니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 36 페이지

검색

게시물 검색