为什么没有显示数据

时间:2016-01-10 13:54:32

标签: php xml sorting

大家好,我现在很烦我

这是我的代码&包含xml文件的链接

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater zainsInflater = LayoutInflater.from(getContext());
    View customView = zainsInflater.inflate(R.layout.img_layout, parent, false);
    String singleImg = getItem(position);
    ImageView imgdb = (ImageView) customView.findViewById(R.id.img_db);
    //Bitmap bmp= BitmapFactory.decodeFile(singleImg);
    //imgdb.setImageBitmap(bmp);
    Picasso.with(getContext()).load(singleImg).resize(50,50).into(imgdb);
    return customView;
}

现在我想要的就是显示它 H1标签中的当前事件名称 然后它列出每个bettype元素与每个下注元素名称和它包含的价格

请帮助我这么近,但真的感觉就像哭了这个哈哈

1 个答案:

答案 0 :(得分:0)

对于XPath表达式,请尝试:

$oCol=$xp->query( '//event[ number( @eventid )="'.$eid.'" ]/bettype/bet' );

or perhaps

$oCol=$xp->query( '//event[ number( @eventid )="'.$eid.'" ]/bettype' );

再次,继续前面的例子:

$eid=5078689.20;/* Tottenham v Leicester */

$events=(object)array(
    'horses'    =>  (object)array(
        'url'   =>  'http://xml.betfred.com/Horse-Racing-Daily.xml',
        'query' =>  '//event/bettype[ number( @bettypeid )="'.$eid.'" ]'
    ),
    'football'  =>  (object)array(
        'url'   =>  'http://xml.betfred.com/football-bonus.xml',
        'query' =>  '//event/bettype'
    ),
    'goals'     =>  (object)array(
        'url'   =>  'http://xml.betfred.com/goals-galore.xml',
        /*'query'   =>  '//event[ number( @eventid )="'.$eid.'" ]/bettype/bet',*/
        'query' =>  '//event[ number( @eventid )="'.$eid.'" ]/bettype'
    )
);

/* the last one will be used */
$obj=$events->horses;
$obj=$events->football;
$obj=$events->goals;


/* create the DOM object & load the xml */
$dom=new DOMDocument;
$dom->load( $obj->url );

$root=$dom->getElementsByTagName('category')->item(0);

/* Create a new XPath object */
$xp=new DOMXPath( $dom );

/* Search the DOM for nodes with particular attribute - bettypeid - use number function from XSLT to test */
$oCol=$xp->query( $obj->query );

/* If the query was successful there should be a nodelist object to work with */
if( $oCol ){

    echo "<div id='bfresults'>";

    foreach( $oCol as $node ) {
        echo '
            <section>
                <h1>'.$node->parentNode->getAttribute('name').' - <span>[ '.$node->getAttribute('name').' ]</span></h1>
                <h2>'.date( 'D, j F, Y H:i:s', strtotime( $node->getAttribute('bet-start-date').' '.$node->parentNode->getAttribute('time') ) ).'</h2>';

        foreach( $node->childNodes as $bet ){
            echo "<div>Name: {$bet->getAttribute('name')} ID: {$bet->getAttribute('id')} Price: {$bet->getAttribute('price')}</div>";
        }
        echo '</section>';
    }
    echo "</div>";

} else {
    echo 'XPath query failed';  
}
$dom = $xp = $col = null;


outputs:

Tottenham v Leicester - [ BTTS + Match Result ]
Sun, 10 January, 2016 16:00:00
Name: Draw ID: 264032028.20 Price: 7/2
Name: Tottenham ID: 264032029.20 Price: 5/2
Name: Leicester ID: 264032030.20 Price: 15/2

Tottenham v Leicester - [ Match Result & Over 2.5 ]
Sun, 10 January, 2016 16:00:00
Name: Draw ID: 264032035.20 Price: 11/1
Name: Tottenham ID: 264032036.20 Price: 6/4
Name: Leicester ID: 264032037.20 Price: 13/2

Tottenham v Leicester - [ Match Result & Under 2.5 ]
Sun, 10 January, 2016 16:00:00
Name: Draw ID: 264032038.20 Price: 7/2
Name: Tottenham ID: 264032039.20 Price: 3/1
Name: Leicester ID: 264032040.20 Price: 17/2