2010-11-23 6 views
2

다음 XML 텍스트를 구문 분석하고 MySQL 데이터베이스에 값을 저장하고 싶습니다. 이 작업을 수행하는 가장 좋은 방법은 무엇입니까?MySQL에 XML 데이터 덤핑

<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); 
관련 문제