programing

SimpleX에서 @attribute 접근ML

sourcejob 2022. 9. 20. 23:55
반응형

SimpleX에서 @attribute 접근ML

접속에 문제가 있다@attribute섹션이 표시됩니다.내가 할 때var_dump오브젝트 전체가 올바른 출력을 얻을 수 있습니다.var_dump오브젝트의 나머지 부분(네스트된 태그)은 올바른 출력을 얻을 수 있지만 문서를 따라가면var_dump $xml->OFFICE->{'@attributes'}빈 객체가 있는데, 첫 번째 객체는var_dump에 출력에 Atribute가 있음을 나타냅니다.

내가 여기서 뭘 잘못하고 있는지 아는 사람? / 내가 어떻게 이 일을 할 수 있는지?

이거 드셔보세요

$xml->attributes()->Token

XML 노드의 attributes() 함수를 호출하면 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";
}

예전에 몇 번을 썼는데@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"]

를 사용합니다.

사실 SimpleXMlement 핸들러는 매우 중요합니다."@attributes"라는 이름의 속성이 없으므로 다음을 수행할 수 없습니다.$sxml->elem->{"@attributes"}["attrib"].

다음 작업을 수행할 수 있습니다.

echo $xml['token'];

이러한 속성 목록을 찾고 있다면 XPath를 참고하십시오.

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

이를 통해 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
)

불행히도 저는 PHP 5.5의 독특한 빌드를 가지고 있습니다(당분간 Gentoo에 고정되었습니다).그리고 제가 발견한 것은

 $xml->tagName['attribute']

유일한 해결책이었습니다.위의 'Right & Quick' 포맷을 포함한 Bora의 모든 방법을 시도했지만 모두 실패했습니다.

이것이 가장 쉬운 포맷이라는 사실은 장점이지만, 다른 사람들이 말하는 모든 포맷을 시도해보고 내가 미쳤다고 생각하는 것을 즐기지 않았다.

그 가치를 즐기다 (독특한 빌드를 언급했는가?)

외부 xml 파일에서 문자열(송 제목과 아티스트 이름만)을 추출합니다.https://nostalgicfm.ro/NowOnAir.xml 다음 형식의 xml:

 <Schedule System="Jazler">
     <Event status="happening" startTime="20:31:20" eventType="song">
        <Announcement Display=""/>
      <Song title="Let It Be ">
       <Artist name="Beatles">
        <Media runTime="265.186"/>
        <Expire Time="20:35:45"/>
       </Artist>
      </Song>
     </Event>
    </Schedule>

나는 이 코드를 PHP로 시도하지만 이름과 제목을 추출하는 방법을 모른다.'Beatles - Let It Be'처럼

  <?php
    $url = "https://nostalgicfm.ro/NowOnAir.xml";
    $xml = simplexml_load_file($url);
    print_r($xml);
    ?>

결과는 Oject:

 SimpleXMLElement Object ( [@attributes] => Array ( [System] => Jazler ) [Event] => SimpleXMLElement Object ( [@attributes] => Array ( [status] => happening [startTime] => 20:51:21 [eventType] => song ) [Announcement] => SimpleXMLElement Object ( [@attributes] => Array ( [Display] => ) ) [Song] => SimpleXMLElement Object ( [@attributes] => Array ( [title] => If You Were A Sailboat ) [Artist] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => Katie Melua ) [Media] => SimpleXMLElement Object ( [@attributes] => Array ( [runTime] => 228.732 ) ) [Expire] => SimpleXMLElement Object ( [@attributes] => Array ( [Time] => 20:55:09 ) ) ) ) ) ) 

스스로 해결:

<?php 
$url = 'https://nostalgicfm.ro/NowOnAir.xml'; 
$xml = simplexml_load_file($url); 
foreach ( $xml->Event->Song->Artist->attributes() as $tag => $value ); 
foreach ( $xml->Event->Song->attributes() as $tag => $value1 ) { 
echo $value." - ".$value1.PHP_EOL; } 
?>

언급URL : https://stackoverflow.com/questions/1652128/accessing-attribute-from-simplexml

반응형