dqm4675 2014-08-22 14:50
浏览 34
已采纳

如何使用php将xml中的节点内容发送到另一个页面?

how to send the content of a node in xml to another page using php?

page.php

    <!DOCTYPE html>
<html>
<head>

<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1  target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<div class="container">
<?php
$html = "";
$url = "http://d-toma.netne.net/Data.xml";
$xml = simplexml_load_file($url);
$title=$xml->page[0]->title;
$image=$xml->page[0]->image;

echo("<div class=\"main\"><img src=\"$image\"/>$title</div>");


  for($i = 1; $i<4;$i++){
$title=$xml->page[$i]->title;
$image=$xml->page[$i]->image;



    $title= $xml->page[$i]->title;
//$title=$xml->page->content->asXML();
$html .="<div class=\"sec\"><img src=\"$image\"/>$title</div>";

 }

echo $html;

?>
</div>

</body>
<html>  

i just want when i click on the div to open another page and put the content of the node i clicked on it please help me thank you i just tryed every thing but i dont know how to solve this problem

  • 写回答

2条回答 默认 最新

  • drti52047 2014-08-22 16:22
    关注

    The next solution is same you require, only I use jQ:

    <!DOCTYPE html>
    <html>
       <body>
        <header>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
            <style>
                .container {
                    position: relative;
                    width: 100%;
                }
    
                .menu {
                    float: left;
                    width: 25%;
                }
    
                .menu a {
                    text-decoration: none;
                }
    
                .page {
                    float: left;
                    width: 75%;
                }
            </style>
        </header>
        <body>
            <div class="container">
                <div class="menu">
                    <ul>
                    </ul>
                </div>
                <div class="page">
                </div>
            </div>
    
            <script>
                var $pageCollection;
    
                function buildMenu($xmlDoc) {
                    var $menu = $('div.menu > ul'),
                        $pageDetail = $('div.page'),
                        items = [];
    
                    // Get page collection
                    $pageCollection = $xmlDoc.find('page');
    
                    $pageCollection.each(function (index, element) {
                        var $page = $(element),
                            item = $('<li><a href="javascript:void(0)" data-index="' + index + '">' 
                                + $page.find('title').html()
                                + '<br />' + $page.find('desc').html() + '</a></li>');
    
                        items.push(item);
                    });
    
                    // Show page 0 for default
                    var $firstPage = $pageCollection.first(),
                        $contentPage = $firstPage.find('content').html();
    
                    $pageDetail.html($contentPage);
    
                    // Add event handlers
                    $menu.append(items);
                    $menu.on('click', 'a', onAnchorClick);
                }
    
                function getXml(url, callback) {
                    $.get(url, function (response) {
                        var $xmlDoc = $(response);
    
                        callback($xmlDoc);
                    });
                }
    
                function onAnchorClick(event) {
                    var $pageDetail = $('div.page'),
                        $anchor = $(event.currentTarget),
                        index = $anchor.attr('data-index');
    
                    // Clear previous content
                    $pageDetail.empty();
    
                    // Show current page
                    var $currentPage = $pageCollection.eq(index),
                        $contentPage = $pageCollection.find('content').html();
    
                    $pageDetail.html($contentPage);
                }
    
                $(function () {
                    var url = 'http://d-toma.netne.net/Data.xml';
    
                    getXml(url, buildMenu);
                });
            </script>
        </body>
    </html>
    

    This solution load using AJAX the XML file, and parse this find for content tag. The anchor in menu div save index position in XML, using it to select current page in XML page collection.

    Regards!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?