SimpleXML is simple to use as its name suggests. 이름이 시사하는로 사용할 simplexml은 간단하다. However it is a pain when you need to check the value of an element which may or may not exist. 그러나 그것은 고통의 값을이 필요한 경우를 확인하는 요소가 존재하지 않을 수도있습니다.

In short using it on XML schema with optional elements is a royal pain. 요컨대에서 스키마를 사용하여 왕실의 아픔과 선택적 요소가있습니다. As SimpleXML only creates objects for which data exists, you will have to write ugly code everytime to check for existence of an object before you access its value. simplexml 전용으로 데이터가 존재하는 객체를 생성, 당신은 추악한 코드를 작성 개체의 존재를 확인하기 위해 매번에 액세스하기 전에 그 값을합니다.

You cannot solve it by simply writing a convenience method. 그것을 간단하게 작성하실 수있습니다 사용자의 편의를 해결할 수없습니다 방법을합니다. Lets understand the problem and then we will solve it. 그리고 우리는이 문제 해결을 통해 그것을 이해합니다.

The Advantage 의 이점
SimpleXML is very simple to use. simplexml은 매우 간단를 사용합니다. You access elements as you would access an Object hierarchy: 요소를 액세스할 수있습니다를 액세스하는 개체를 계층 구조 :
$xml->Items[0]->Request[0]->Errors[0]->Error[0]->Status $ xml -> 상품 [0] -> 요청 [0] -> 오류가 [0] -> 오류 [0] -> 현재 상태

The Problem 이 문제
SimpleXML creates objects only for XML elements which have data (and rightly so). 객체를 생성 simplexml있는 요소에 대해서만 데이터를 (그리고 바르게 남서). This makes it efficient. 이렇게하면 효율적으로합니다. However it also makes it harder to use when your schema is flexible with optional elements. 그러나 또한 스키마는 것을 어렵게 만듭니다 유연한 옵션을 사용하면 요소를합니다. Lets take the above example. 위 예제를 사용 타고합니다. You cannot safely access Status as above if Errors is an optional element which is only present when there is an error. 위와 같이 현재 상태를 안전하게 액세스할 수 없다는 선택 사항이 오류가있는 요소에 오류가있는 경우에만 존재합니다. And you cannot check the existence of Status like this: 그리고 당신의 존재를 확인할 수없습니다 상황 이렇게 :
isset($xml->Items[0]->Request[0]->Errors[0]->Error[0]->Status) isset ($ xml -> 상품 [0] -> 요청 [0] -> 오류가 [0] -> 오류 [0] -> 상태)
because Errors may not be present. 때문에 오류가 제시되지 않을 수있습니다. So to find out if there are any error you first have to check the existence of Errors and then the existance of Status (possibly) before you can actually check the status. 그래서하지 않은 상태에서 오류가 있는지 확인하려면 당신의 존재를 확인하는 첫 번째는 오류 및 다음의 존재 상태 (아마)하기 전에 실제로 상태를 확인하실 수있습니다. And this is true for any depth of the object hierarchy. 그리고 이것은 사실이 개체 계층 구조의 깊이를 찾으세요. You can see how quickly this cane become a major pain in the neck. 이 지팡이가 얼마나 빨리 볼 수있습니다 목에 통증을 전공합니다. Also you are very likely to miss some and cause maintenance hassles. 또한 당신을 일으킬 가능성이 매우 높다는 유지 보수 열띤 논쟁 일부를 그리워합니다. The code becomes unmaintainable too. 이 코드는 unmaintainable 너무합니다.

Solution 해결 방법
My solution is to use XPath for such situations. 내 솔루션은 이러한 상황을 xpath를 사용합니다. It succintly tells you the availability and value (if available) of a field. 그것 succintly 알려주의 가용성 및 값 (해당되는 경우)의 필드.

Conclusion 결론
SimpleXML in conjunction with XPath is a very powerful way to process your XML data in PHP. xpath와 함께 simplexml은 매우 강력한 방법을 처리할 수 xml 데이터를 php합니다.