Here is a more namespace-aware version of the earlier function for attaching one SimpleXMLElement to another. I'm sure it could still be further improved (right now it generates some redundant xmlns definitions), but it seems to be working well enough for my purposes so far.
function SimpleXMLElement_append($parent, $child)
{
// get all namespaces for document
$namespaces = $child->getNamespaces(true);
// check if there is a default namespace for the current node
$currentNs = $child->getNamespaces();
$defaultNs = count($currentNs) > 0 ? current($currentNs) : null;
$prefix = (count($currentNs) > 0) ? current(array_keys($currentNs)) : '';
$childName = strlen($prefix) > 1
? $prefix . ':' . $child->getName() : $child->getName();
// check if the value is string value / data
if (trim((string) $child) == '') {
$element = $parent->addChild($childName, null, $defaultNs);
} else {
$element = $parent->addChild(
$childName, htmlspecialchars((string)$child), $defaultNs
);
}
foreach ($child->attributes() as $attKey => $attValue) {
$element->addAttribute($attKey, $attValue);
}
foreach ($namespaces as $nskey => $nsurl) {
foreach ($child->attributes($nsurl) as $attKey => $attValue) {
$element->addAttribute($nskey . ':' . $attKey, $attValue, $nsurl);
}
}
// add children -- try with namespaces first, but default to all children
// if no namespaced children are found.
$children = 0;
foreach ($namespaces as $nskey => $nsurl) {
foreach ($child->children($nsurl) as $currChild) {
SimpleXMLElement_append($element, $currChild);
$children++;
}
}
if ($children == 0) {
foreach ($child->children() as $currChild) {
SimpleXMLElement_append($element, $currChild);
}
}
}
像是 $xml->result->title; 取回的資料會是 SimpleXML 的物件..並非 String 或是 Int ...
有時候在傳入其他參數或是 mysql 的時候, 最好要強迫型態轉換.
感恩, 我沒注意到居然是 object, 馬上補上~
您好:
我可以請教您一個關於如何將xml的資料,匯入mysql的資料庫的問題嗎?
把 XML parse 成 array, 再寫個 foreach 寫入 MySQL 就好了呀.
您好:
如果,我可以給我將檔案寄給您,您可以幫我將如何做,才能把這個xml檔,匯入sql的的操作說明,錄製成操作錄影檔嗎?
需要多少費用?
我的msn:ut5591066@hotmail.com
skype:ut5553616 富爸爸水電工
你先寄一部份 XML 和要 匯入DB 的 schema 給我看看?
順便問問這個的用途是? 商用? or ?
我先把xml寄給您。
我是要做商品目錄,做成產品鏈結,幫廠商賣產品,賣出去就可以抽佣金。(類似通路王)
因為,廠商的產品太多了,而且廠商有提供xml檔。
至於資料庫,因為,我目前是用osc的。
如果,用osc的不好做,可以另外開一個新的資料庫。
Blog 左邊列有 My Mail, 可以先寄一些讓我看看, 還有要對應哪些欄位等等.
您好~我想請教
如果我透過post的方式取得遠端xml的值 然後使用
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3); //times out after 4s
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
$result=curl_exec($ch);
print_r($result);
的方式
輸出的值會是 張先生chung@aaa.com台北市松山區......這樣黏在一起
請問我該如何各別取得變數植呢?
例如 $name="張先生";
$email="chung@aaa.com";
不知道前輩是不是可以幫我解答?
感謝
依照你最前面講得 "取得遠端xml的值", 那你現在的 $result 應該是 XML, 而不是連在一起的字串.
print_r(simplexml_load_string($result));
就可以了.
前輩 使用了您說的方法 是可以列出所有的值 如下列
SimpleXMLElement Object ( [errorCode] => SimpleXMLElement Object ( ) [userLoginStatus] => true [userAccount] => chung@teme.biz [userId] => 127081 [userName] => 張先生 [userNickname] => chung [userTel] => 02-27658888 [userMobile] => 0920-555-265
但如果我想要取得單獨欄位值我該怎麼下指令
例如我只想要得到[userNickname]
抱歉問了好像很笨的問題...
但還有希望前輩能夠給予指導
不勝感激
$x = simple...($result);
echo $x->userNickname;
這些已經是基本能力了, 建議您在加強點基礎.
謝謝前輩...我會的..感謝