2015-01-11 3 views
0

안녕 얘들 아 XML을 구문 분석하고 PHP로 저장하려고 해요? (중간에 있어요). 이미 XML을로드하고 표시 할 데이터 만 표시합니다. 어떻게 할 수 있습니까? 나는 XML을 저장합니까? 어떻게 XML을로드하고 PHP로 저장할 수 있습니까?

내 코드입니다 :

<?php 

$html = ""; 
$url = "http://www.conciencia.net/rss.aspx"; 
$xml = simplexml_load_file($url); 

$channel_title = $xml->channel->title; 
$channel_link = $xml->channel->link; 
$managingEditor = $xml->channel->managingEditor; 
$channel_description = $xml->channel->description; 
$lbd = $xml->channel->lastBuildDate; 

$html .= "<br/>$channel_title"; 
$html .= "<br/>$channel_link"; 
$html .= "<br/>$managingEditor"; 
$html .= "<br/>$lbd"; 
$html .= "<br/>$channel_description<br/>"; 

for($i = 0; $i < 7; $i++){ 

    $pubDate = $xml->channel->item[$i]->pubDate; 
$title = $xml->channel->item[$i]->title; 
$link = $xml->channel->item[$i]->link; 
    $guid = $xml->channel->item[$i]->guid; 
    $author = $xml->channel->item[$i]->author; 
$description = $xml->channel->item[$i]->description; 
    $url0 = $xml->channel->item[$i]->enclosure->attributes()->url; 

    for($ii = 0; $ii < 2; $ii++){ 
    $url = $xml->channel->item[$i]->enclosure[$ii]->attributes()->url; 
    } 

    $html .= "<br/>$pubDate";  
    $html .= "<a href='$link'><h3>$title</h3></a>"; 
    $html .= "<br/>$guid"; 
    $html .= "<br/>$author"; 
    $html .= "<br/>$description"; 
    $html .= "<br/>$url0"; 
    $html .= "<br/>$url"; 
    $html .= "<br/>$link<br/>"; 
} 

echo $html; 

?> 

그 코드 작업 좋은, 난 그냥 표시 할 XML에서 태그를로드 는 내가 그 후하고 싶은 것은 다음과 같습니다의 (a XML을 만드는 것입니다 구조화)하고 데이터를 저장하십시오. 어떻게 할 수 있습니까?

myxml.xml

<channel> 
       <title>$channel_title</title> 
       <link>$channel_link</link> 
       <managingEditor>$managingEditor</managingEditor> 
       <channel_description>$channel_description</channel_description> 
       <item> 
         <title>$title</title> 
         <guid>$guid</guid> 
         <author>$author</author> 
         <description>$description</description> 
         <enclosure0>$url0</enclosure0> 
         <enclosure1>$url</enclosure> 

       </item> 
       <item> 
         <title>$title</title> 
         <guid>$guid</guid> 
         <author>$author</author> 
         <description>$description</description> 
         <enclosure0>$url0</enclosure0> 
         <enclosure1>$url</enclosure> 

       </item> 
       <item> 
         <title>$title</title> 
         <guid>$guid</guid> 
         <author>$author</author> 
         <description>$description</description> 
         <enclosure0>$url0</enclosure0> 
         <enclosure1>$url</enclosure> 

       </item> 
       <item> 
         <title>$title</title> 
         <guid>$guid</guid> 
         <author>$author</author> 
         <description>$description</description> 
         <enclosure0>$url0</enclosure0> 
         <enclosure1>$url</enclosure> 

       </item> 
       <item> 
         <title>$title</title> 
         <guid>$guid</guid> 
         <author>$author</author> 
         <description>$description</description> 
         <enclosure0>$url0</enclosure0> 
         <enclosure1>$url</enclosure> 

       </item> 
       <item> 
         <title>$title</title> 
         <guid>$guid</guid> 
         <author>$author</author> 
         <description>$description</description> 
         <enclosure0>$url0</enclosure0> 
         <enclosure1>$url</enclosure> 

       </item> 
       <item> 
         <title>$title</title> 
         <guid>$guid</guid> 
         <author>$author</author> 
         <description>$description</description> 
         <enclosure0>$url0</enclosure0> 
         <enclosure1>$url</enclosure> 

       </item> 
</channel> 
+0

변수를 HTML로 이스케이프 처리하려면 'htmlspecialchars()'를 사용해야합니다. –

+0

http://stackoverflow.com/questions/3418376/how-to-save-changed-simplexml-object-back-to-file –

+0

의 가능한 복제물 xml을 작성해야하는 이유는 무엇입니까? 당신은 실제로 아무것도 수정하지 않았습니다. 방금 일부 XML 속성을 가져 와서 HTML로 출력합니다. – r3wt

답변

0

에게있다 그럼 당신은 멍청 할 수 있어요. 일반 문자열 연결을 사용하여 XML 파일을 작성하십시오 (그렇지 않은 경우 일부는 더 적절하다고 생각되는 xml 객체로 어렵게 시간을 보낼 수 있습니다).

다음은 쓰기 모드에서 fopen()으로 파일을 엽니 다. 파일이 존재하지 않으면 생성됩니다. 해당 디렉토리에 쓸 권한이 있는지 확인하십시오. 그렇지 않으면 파일이 기록되지 않습니다 (오류보고가 엄격한 경우 치명적인 오류가 발생하고 경고 만 표시됨). 둘째, 당신은 XML로 문자열을로드 할 수 simplexml_load_string()를 사용할 수 있습니다, 또는, file.for 추가 정보 read the manual on filesystem functions

$file = '/path/to/mynewxmlfile.xml'; 
$data = 
' 
<?xml version="1.0"?> 
<channel> 
<title>'.$channel_title.'</title> 
<link>'.$channel_link.'</link> 
<managingEditor>'.'$managingEditor.</managingEditor> 
<channel_description>'.$channel_description.'</channel_description> 
'; 

for($i = 0; $i < 7; $i++){ 

    $pubDate = $xml->channel->item[$i]->pubDate; 
    $title = $xml->channel->item[$i]->title; 
    $link = $xml->channel->item[$i]->link; 
    $guid = $xml->channel->item[$i]->guid; 
    $author = $xml->channel->item[$i]->author; 
    $description = $xml->channel->item[$i]->description; 
    $url0 = $xml->channel->item[$i]->enclosure->attributes()->url; 

    for($ii = 0; $ii < 2; $ii++){ 
     $url = $xml->channel->item[$i]->enclosure[$ii]->attributes()->url; 
    } 
    $data .= 
    ' 
    <item> 
    <title>'.$title.'</title> 
    <guid>'.$guid.'</guid> 
    <author>'.$author.'</author> 
    <description>'.$description.'</description> 
    <enclosure0>'.$url0.'</enclosure0> 
    <enclosure1>'.$url.'</enclosure1> 
    </item> 
    '; 

} 
$data .= '</channel>'; 

fwrite(fopen($file,'w'),$data); 

를 작성하는 fwrite()를 사용하여, 다음과 같이, 디스크에 기록 할 asXml()를 사용 :

$data = simplexml_load_string($data); 
$data->asXml($file); 
+1

고마워요, 당신이 남자 야!, 그거야!, .. 나는 단지 ": eacute;"(html 엔티티 내가 믿는다)를 디코딩하는 방법을 알아 내야 만한다. 어떤 생각? –

1

워드 프로세서 http://php.net/manual/en/simplexmlelement.asxml.php

$xml->asXml('rss.xml'); 

편집

를 읽고 새 XML을 만들려면 응답 https://stackoverflow.com/a/143192/1742977

+1

docs에서 참조 : "filename 지정된 경우 함수는 파일을 반환하지 않고 파일에 데이터를 씁니다." 사용자 게시물에서 참조 : "어떻게 XML을 저장할 수 있습니까?" 내가 틀린 곳? – Gregsparrow

+0

안녕 얘들 아, 내가 질문을 업데이 트했습니다 더 많은 코드를 추가, 내 질문에 더 명확하게 노력하고, 내가 뭘하려고하는지. –

관련 문제