簡體   English   中英

無法從xml文件獲取值

[英]Having trouble getting values from an xml file

當我嘗試獲取價值時,它似乎不起作用。 xml文件包含相同節點<post>倍數,我認為您可以像數組一樣調用特定節點,但這似乎不起作用。 下面我包含了代碼。

獲取xml值的文件:

<?php

if(file_exists('settings.xml')){
    $settings = simplexml_load_file('settings.xml');

    $site_title = $settings->title;
    $site_name = $settings->title;
    $site_stylesheet = "css/main.css";

    $theme_folder = "themes/".$settings->theme;
    if(file_exists('posts.xml')){
        $posts = simplexml_load_file('posts.xml');

        $post = $posts->post[$_GET['post']];

        $post_title = $post->title;
        $post_content = $post->content;
        $post_author = $post->author;


        include($theme_folder."/header.php");
        include($theme_folder."/post.php");
        include($theme_folder."/footer.php");
    } else {
        exit("File \"posts.xml\" does not exist!");
    }
} else {
    exit("File \"settings.xml\" does not exist!");  
}

?>

上面的文件中包含的文件,該文件使用xml傳遞給的變量:

<article>
    <h1><?php echo $post_title ?></h1>
    <?php echo $post_content ?>
    <p><?php echo $post_author ?></p>
</article>

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<posts>
    <post>
        <title>Hello World</title>
        <author>tacticalsk8er</author>
        <content>Hello world this is my blogging site</content>
    </post>
    <post>
        <title>Hello Again</title>
        <author>Nick Peterson</author>
        <content><![CDATA[Hello Again world this is another test for my <b>blogging site</b>]]></content>
    </post>
</posts>

當然,您可以使用simplexml array-style '訪問節點:

$post = $xml->post[1]; // select second node
echo $post->title;

$xml表示根節點<posts>$xml->post選擇posts/post

看到它正常工作:http: //3v4l.org/KOiv2

請參閱手冊: http : //www.php.net/manual/zh/simplexml.examples-basic.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM