2010-11-23 10 views
2

次のXMLテキストを解析し、その値をMySQLデータベースに保存します。これを行う最善の方法は何ですか?XMLデータをMySQLにダンプする

<urn:outgoing soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
    <pob xsi:type="out:OutgoingTransactionRqstInfo" 
    xmlns:out="http://www.shantanuoak.com/OutgoingService"> 
    <messageID xsi:type="xsd:int">9999</messageID> 
    <instCode xsi:type="soapenc:string" 
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">?</instCode> 
    </pob> 
</urn:outgoing> 
+0

@shantanuoような何かを行うことができます - 期待される形式とは何ですか?そして、プログラミング言語を好む? – ajreal

+0

MySQLの挿入文。 PHPまたはJAVA – shantanuo

+0

あなたのスキーマは次のように見えますか?どのような価値を持ちたいと思いますか? – netcoder

答えて

2

あなたのxmlファイル内のすべての名前空間を無視して、あなたは

class XmlToDb { 
    private $data = array(); 
    private $currentTagname; 

    private function parseXML($xmlContent) { 
     $xmlParser = xml_parser_create(); 
     xml_set_character_data_handler($xmlParser, array($this, 'contentElement')); 
     xml_set_element_handler($xmlParser, array($this, "startElement"), array($this, "endElement")); 
     if(!xml_parse($xmlParser, $xmlContent)){ 
      die("Error on line " . xml_get_current_line_number($xmlParser)); 
     } 
     xml_parser_free($xmlParser); 
    } 
    public function save($xml) { 
     $this->parseXML($xml); 

     $sql = "INSERT INTO [table] VALUES ('" . $this->data['MESSAGEID'] . "', '" . $this->data['INSTCODE'] . "')"; 
     echo $sql; 
    } 

    private function contentElement($parser, $data) { 
     $data = trim($data); 
     if (!empty($data)) { 
      $this->data[$this->currentTagname] = $data; 
     } 
    } 

    private function startElement($parser, $tag, $attributeList) { 
     if ($tag == 'MESSAGEID') { 
      // @todo add specific validation to this element. e.g. make sure its valid number 
     } else if ($tag == 'INSTCODE') { 
     } else { 
     } 
     $this->currentTagname = $tag; 
    } 
    private function endElement($parser, $tag) { 
     return ''; 
    } 
} 

$xmlContent= ' 
<urn:outgoing xmlns:env="http://www.w3.org/2003/05/soap-envelope"> 
    <pob xsi:type="out:OutgoingTransactionRqstInfo" 
    xmlns:out="http://www.shantanuoak.com/OutgoingService"> 
    <messageID xsi:type="xsd:int">9999</messageID> 
    <instCode xsi:type="soapenc:string" 
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">?</instCode> 
    </pob> 
</urn:outgoing> 
'; 
$xmlToDb = new XmlToDb(); 
$xmlToDb->save($xmlContent); 
関連する問題