我正在为大学演讲做一个项目,现在我正在寻找一个超过 2 周的解决方案,但我就是做错了。
我们有一个项目,我们需要生成特定的 JSON 或 XML 文件,以便稍后使用 D3 或 Sigma 将它们可视化。
我们有一个 mysql 数据库,所有代码都在 Javascript 中(正如您在库中看到的那样),我们使用 pho 从数据库中获取数据并以正确的格式获取数据。这是我尝试使用 php 创建的示例 xml 文件(它是使用 Sigma 进行可视化的 gexf 文件,但它与 xml 相同):
<?xml version="1.0" encoding="UTF-8"?>
<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2">
<meta lastmodifieddate="2009-03-20">
<creator>Gexf.net</creator>
<description>A hello world! file</description>
</meta>
<graph mode="static" defaultedgetype="directed">
<nodes>
<node id="0" label="Hello" />
<node id="1" label="Word" />
</nodes>
<edges>
<edge id="0" source="0" target="1" />
</edges>
</graph>
</gexf>
这是我尝试创建 xml 的 php 代码:
<?php
set_time_limit(500000000);
ini_set('memory_limit', '-1');
class XmlWriter2 {
var $xml;
var $indent;
var $stack = array();
function XmlWriter($indent = ' ') {
$this->indent = $indent;
$this->xml = '<?xml version="1.0" encoding="utf-8"?>'."\n";
}
function _indent() {
for ($i = 0, $j = count($this->stack); $i < $j; $i++) {
$this->xml .= $this->indent;
}
}
function push($element, $attributes = array()) {
$this->_indent();
$this->xml .= '<'.$element;
foreach ($attributes as $key => $value) {
$this->xml .= ' '.$key.'="'.htmlentities($value).'"';
}
$this->xml .= ">\n";
$this->stack[] = $element;
}
function element($element, $content, $attributes = array()) {
$this->_indent();
$this->xml .= '<'.$element;
foreach ($attributes as $key => $value) {
$this->xml .= ' '.$key.'="'.htmlentities($value).'"';
}
$this->xml .= '>'.htmlentities($content).'</'.$element.'>'."\n";
}
function emptyelement($element, $attributes = array()) {
$this->_indent();
$this->xml .= '<'.$element;
foreach ($attributes as $key => $value) {
$this->xml .= ' '.$key.'="'.htmlentities($value).'"';
}
$this->xml .= " />\n";
}
function pop() {
$element = array_pop($this->stack);
$this->_indent();
$this->xml .= "</$element>\n";
}
function getXml() {
return $this->xml;
}
}
/*
$xml = new XmlWriter2();
$array = array(
array('monkey', 'banana', 'Jim'),
array('hamster', 'apples', 'Kola'),
array('turtle', 'beans', 'Berty'),
);
$xml->push('zoo');
foreach ($array as $animal) {
$xml->push('animal', array('species' => $animal[0]));
$xml->element('name', $animal[2]);
$xml->element('food', $animal[1]);
$xml->pop();
}
$xml->pop();
print $xml->getXml();
<?xml version="1.0" encoding="utf-8"?>
<zoo>
<animal species="monkey">
<name>Jim</name>
<food>banana</food>
</animal>
<animal species="hamster">
<name>Kola</name>
<food>apples</food>
</animal>
<animal species="turtle">
<name>Berty</name>
<food>beans</food>
</animal>
</zoo>
*/
mysql_connect("127.0.0.1", "root", "manager") or die(mysql_error());
mysql_select_db("enrondata") or die(mysql_error());
$data1 = mysql_query("SELECT DISTINCT sId FROM mailToId WHERE date
BETWEEN '01.06.2002' AND '30.06.2002' UNION SELECT DISTINCT rId FROM
mailToId WHERE date BETWEEN '01.06.2002' AND '30.06.2002'") or die
(mysql_error());
$data2 = mysql_query("SELECT sender, recipient, count(*) AS numMails
FROM mailTo WHERE date BETWEEN '01.06.2002' AND '30.06.2002' GROUP
BY sender, recipient") or die (mysql_error());
$users = array();
$id = 0;
while($tmpUsers = mysql_fetch_array($data1)){
$tmpArray['id'] = $tmpUsers['sId'];
$user = mysql_query("SELECT email FROM users WHERE id=".$tmpUsers['sId']);
while($tmpUser = mysql_fetch_array($user)){
$tmpArray['email'] = $tmpUser['email'];
}
array_push($users, $tmpArray);
}
$xml = new XmlWriter2();
$xml->push('gexf', array('xmlns' => 'http://www.gexf.net/1.2draft" version="1.2'));
$xml->push('meta', array('lastmodifieddate' => '2009-03-20'));
$xml->element('creator', 'Gexf.net');
$xml->element('description', 'A hello world! file');
$xml->pop();
$xml->push('graph', array('mode' => 'static', 'defaultedgetype' => 'directed'));
$xml->push('nodes');
for($i = 0; $i < count($users); $i++){
$xml->push('node', array('id' => $users['id'],
'label' => $users['email']));$xml->pop();
}
$xml->pop();
$xml->push('edges');
while($tmp = mysql_fetch_array($data2)){
$xml->push('edge', array('id' => id,
'source' => $tmp['sender'], 'target' => $tmp['recipient'], 'weight'
=> $tmp['numMails']));$xml->pop();
$id++;
}
$xml->pop();
$xml->pop();
$xml->pop();
print $xml->getXml();
?>
它有效,代码正确,但需要几个小时。真的,即使在 30 分钟之后,它还没有完成所有这些。而且我不知道如何改进它并快速获得它。或者是否有另一种可能在不使用 php 的情况下以正确的格式从 mysql 数据库中获取数据?
请帮我。我的截止日期真的很近,我没有任何想法,也没有在网上找到任何适合我的问题的东西。