简体   繁体   English

无法从xml文件获取值

[英]Having trouble getting values from an xml file

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. xml文件包含相同节点<post>倍数,我认为您可以像数组一样调用特定节点,但这似乎不起作用。 Below I have included the code. 下面我包含了代码。

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文件:

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

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表示根节点<posts>$xml->post选择posts/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/zh/simplexml.examples-basic.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM