라이브러리

[PHP] XMLReader::moveToAttributeNo - 인덱스로 커서를 속성으로 이동합니다.




XMLReader::moveToAttribute


XMLReader 클래스는 XML 문서를 읽기 위한 클래스입니다. 이 클래스를 사용하여 XML 문서의 특정 노드, 속성, 또는 속성의 값을 읽을 수 있습니다.

`moveToAttribute` 메서드는 XMLReader 객체가 현재 위치한 노드의 특정 속성을 읽을 수 있도록 합니다. 이 메서드는 XMLReader 객체의 현재 위치를 변경하지 않습니다.

# 예제


#hostingforum.kr
php

$xml = new DOMDocument();

$xml->loadXML('');

$xmlReader = new XMLReader();

$xmlReader->XML($xml->saveXML());



// 현재 위치를 root 노드로 이동

$xmlReader->moveToElement();



// id 속성으로 이동

$xmlReader->moveToAttribute('id');



// id 속성의 값을 읽어옵니다.

echo $xmlReader->readString() . "
"; // 1



// name 속성으로 이동

$xmlReader->moveToAttribute('name');



// name 속성의 값을 읽어옵니다.

echo $xmlReader->readString() . "
"; // John



// age 속성으로 이동

$xmlReader->moveToAttribute('age');



// age 속성의 값을 읽어옵니다.

echo $xmlReader->readString() . "
"; // 30



# moveToAttributeNo


`moveToAttributeNo` 메서드는 `moveToAttribute` 메서드와 유사하지만, 현재 위치가 속성 노드에 있으면 속성 노드의 인덱스를 반환하고, 속성 노드에 있지 않으면 0을 반환합니다.

# 예제


#hostingforum.kr
php

$xml = new DOMDocument();

$xml->loadXML('');

$xmlReader = new XMLReader();

$xmlReader->XML($xml->saveXML());



// 현재 위치를 root 노드로 이동

$xmlReader->moveToElement();



// id 속성으로 이동

$xmlReader->moveToAttribute('id');



// id 속성의 인덱스를 읽어옵니다.

echo $xmlReader->moveToAttributeNo() . "
"; // 0



// name 속성으로 이동

$xmlReader->moveToAttribute('name');



// name 속성의 인덱스를 읽어옵니다.

echo $xmlReader->moveToAttributeNo() . "
"; // 1



// age 속성으로 이동

$xmlReader->moveToAttribute('age');



// age 속성의 인덱스를 읽어옵니다.

echo $xmlReader->moveToAttributeNo() . "
"; // 2



# 결론


`moveToAttribute` 메서드는 XMLReader 객체가 현재 위치한 노드의 특정 속성을 읽을 수 있도록 합니다. `moveToAttributeNo` 메서드는 `moveToAttribute` 메서드와 유사하지만, 현재 위치가 속성 노드에 있으면 속성 노드의 인덱스를 반환하고, 속성 노드에 있지 않으면 0을 반환합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색