IT

SimpleXML에서 @attribute에 액세스

lottoking 2020. 8. 4. 22:48
반응형

SimpleXML에서 @attribute에 액세스


@attributeSimpleXML 수업 섹션에 액세스하는 데 문제가 있습니다. 언제가 var_dump전체 개체, 나는 정확한 출력을 얻을 때, I var_dump개체 (중요한 태그), 내가 올바른 출력을 얻을 수있는 문서를 따라 할 때의 나머지 var_dump $xml->OFFICE->{'@attributes'}사실에도 불구하고, 나는 빈 개체를 그럼에도 수 첫 번째 var_dump는 출력 할 속성이 있음을 분명히 보여줍니다.

누가 어디에서 뭘 잘못하고 있는지 / 어떻게이 일을 할 수 있습니까?


XML 노드에서 속성 () 함수를 호출하여 XML 요소의 속성을 얻을 수 있습니다. 그런 다음 함수의 반환 값을 var_dump 할 수 있습니다.

php.net http://php.net/simplexmlelement.attributes에 대한 추가 정보

해당 페이지의 예제 코드 :

$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}

이 시도

$xml->attributes()->Token

나는 아래 @attributes처럼 되기 위해 여러 번 사용 했지만 조금 더 길었습니다.

$att = $xml->attributes();
echo $att['field'];

더 업그레이드 야하며 한 번에 다음 형식 만 속성을 얻을 수 있습니다.

표준 방식-배열 액세스 속성 (AAA)

$xml['field'];

다른 대안은 다음과 가변합니다.

오른쪽 및 빠른 형식

$xml->attributes()->{'field'};

잘못된 형식

$xml->attributes()->field;
$xml->{"@attributes"}->field;
$xml->attributes('field');
$xml->attributes()['field'];
$xml->attributes->['field'];

$xml = <<<XML
<root>
<elem attrib="value" />
</root>
XML;

$sxml = simplexml_load_string($xml);
$attrs = $sxml->elem->attributes();
echo $attrs["attrib"]; //or just $sxml->elem["attrib"]

사용하십시오 .SimpleXMLElement::attributes

사실, SimpleXMLElement 는 시간이 많이 사용되고 있습니다. "@attributes"라는 속성이 내부 수행 할 수 없습니다 .get_properties$sxml->elem->{"@attributes"}["attrib"]


당신은 할 수 있습니다 :

echo $xml['token'];

소유 속성 목록을 소유하고 XPath가 친구가 될 것입니다.

print_r($xml->xpath('@token'));

불행히도 PHP 5.5의 독창적 인 빌드 (현재 Gentoo와 함께 제공됨)가 있습니다.

 $xml->tagName['attribute']

작동 한 유일한 솔루션. 위의 'Right & Quick'형식을 포함하여 위의 모든 Bora 방법을 시도했지만 모두 실패했습니다.

이것이 가장 쉬운 형식이라는 사실은 장점이지만 다른 사람들이 말한 모든 형식을 시도하는 것이 제 정신이 아니었다 고 생각하는 것을 좋아하지 않았습니다.

Njoy의 가치는 무엇입니까 (독특한 빌드를 언급 했습니까?).


simplexml_load_file ($ file)의 결과를 JSON 구조로 변환하고 다시 디코딩하는 데 도움이되었습니다.

$xml = simplexml_load_file("$token.xml");
$json = json_encode($xml);
$xml_fixed = json_decode($json);

$try1 = $xml->structure->{"@attributes"}['value'];
print_r($try1);

>> result: SimpleXMLElement Object
(
)

$try2 = $xml_fixed->structure->{"@attributes"}['value'];
print_r($try2);

>> result: stdClass Object
(
    [key] => value
)

참고 URL : https://stackoverflow.com/questions/1652128/accessing-attribute-from-simplexml

반응형