无法从xml文件中获取值

时间:2020-12-19 02:06:07

When I try to get values it doesn't seem to work. The xml file contains multiples of the same node <post> and I thought you could call a specific node like an array, but that doesn't seem to work. Below I have included the code.

当我试图获得价值时,它似乎不起作用。 xml文件包含同一个节点 的多个,我认为你可以像一个数组一样调用一个特定的节点,但这似乎不起作用。下面我已经包含了代码。

File that gets xml values:

获取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!");  
}

?>

File that is included in the file above and uses the variables that the xml passes to:

上面文件中包含的文件,并使用xml传递给的变量:

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

Xml file:

<?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>

1 个解决方案

#1


0  

Of course, you can access nodes in simplexml 'array-style':

当然,您可以使用simplexml“array-style”访问节点:

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

$xml represents the root-node <posts>, $xml->post selects posts/post.

$ xml表示根节点 ,$ xml-> post选择帖子/帖子。

see it working: http://3v4l.org/KOiv2

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

see the manual: http://www.php.net/manual/en/simplexml.examples-basic.php

请参阅手册:http://www.php.net/manual/en/simplexml.examples-basic.php

#1


0  

Of course, you can access nodes in simplexml 'array-style':

当然,您可以使用simplexml“array-style”访问节点:

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

$xml represents the root-node <posts>, $xml->post selects posts/post.

$ xml表示根节点 ,$ xml-> post选择帖子/帖子。

see it working: http://3v4l.org/KOiv2

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

see the manual: http://www.php.net/manual/en/simplexml.examples-basic.php

请参阅手册:http://www.php.net/manual/en/simplexml.examples-basic.php