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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里