xml 파싱 도움좀 주세요..

· 2012-04-06 (금) 14:47:29 · 3495 · 1
11번가 파싱을 하고있습니다..

파싱법을 정확하게 모르고 구글링으로 할려고 하니깐 잘 모르겠네요..

xml은 참고소스형식으로 불러 옵니다.


메인 페이지에서
$xmlParser = new XMLParser();
$parser = @xml_parser_create();
if (!is_resource($parser)){
die("PHP XML parser에러");
} else {
xml_set_object($parser, $xmlParser);
xml_set_element_handler($parser, "startHandler", "endHandler");
xml_set_character_data_handler($parser, "cdataHandler");
}

if (!xml_parse($parser, $body, true)) {
printf("XML error: %s at line %d\n",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser));
}

if (is_resource($parser)) {
xml_parser_free($parser);
unset( $parser );
}


이런식으로 해놓았습니다.

xml_set_element_handler($parser, "startHandler", "endHandler");
xml_set_character_data_handler($parser, "cdataHandler");

이부분이 정확하게 어떻게 처리하는지를 모르겠네요.




class XMLParser {
var $inItem = FALSE;
var $currentElement = FALSE;
var $itemInfo = array();
var $cdata = "";

function startHandler($parser, $element, $attr) {
switch ($element) {
case "ITEM":
$this->inItem = TRUE;
break;

case "TITLE" :
case "AUTHOR" :
case "COVER" :
case "PUBLISHER" :
if ($this->inItem) {
$this->currentElement = $element;
$this->cdata = "";
}
break;

default:
$this->currentElement = FALSE;
break;
}
}

function endHandler($parser, $element) {
if ($element == "ITEM") {
$this->inItem = FALSE;
$this->currentElement = FALSE;
}

if ($this->currentElement) {
$this->itemInfo[$this->currentElement] = $this->cdata;
$this->currentElement = FALSE;
$this->cdata = "";
}
}


function cdataHandler($parser, $cdata){
  if($this->inItems==TRUE){
if($this->currentElement=="PRODUCTNAME"){
$this->itemInfo["PRODUCTNAME"] = $cdata;
} else if($this->currentElement=="PRODUCTIMAGE"){
$this->itemInfo["PRODUCTIMAGE"] = $cdata;
} else if($this->currentElement=="DETAILPAGEURL"){
$this->itemInfo["DETAILPAGEURL"] = $cdata;
}   
  }
}

}

구글링해서 얻은 원본 소스는 이거인데.. 어디를 수정해야 하는지 도움좀 주십시요.
 
 
 
///////////////xml
- <ProductSearchResponse>
02   - <Request>
03     - <Arguments>
04       <Argument name="key" value="234" /> 
05       <Argument name="apiCode" value="ProductSearch" /> 
06       <Argument name="keyword" value="cp671 225/55R17" /> 
07      </Arguments>
08      <ProcessingTime>0.023 sec</ProcessingTime> 
09   </Request>
10   
11 - <Products> 
12     <TotalCount>8</TotalCount> 
13   
14 - <Product>  <==이부분 부터 물품 반복
15   <ProductCode>168246154</ProductCode> 
16 - <ProductName>
17 - <![CDATA[ 넥센타이어 NEXEN CP671 [225/55R17] 중대형 세단에 적합한 사계절 패턴 디자인, 넥센 고급 타이어, 제조일로 부터 3~4개월 이내 타이어, 왕도매 타이어
18   ]]> 
19   </ProductName>
20   <ProductPrice>90800</ProductPrice> 
21 - <ProductImage>
22 - <![CDATA[ http://i.011st.com/t/080/i/2/4/6/1/5/4/168246154_B_6.jpg
23   ]]> 
24   </ProductImage>
25 - <ProductImage100>
26 - <![CDATA[ http://i.011st.com/t/100/i/2/4/6/1/5/4/168246154_B_6.jpg
27   ]]> 
28   </ProductImage100>
29 - <Text1>
30 - <![CDATA[  
31   ]]> 
32   </Text1>
33 - <Text2>
34 - <![CDATA[  
35   ]]> 
36   </Text2>
37 - <SellerNick>
38 - <![CDATA[ 왕도매타이어
39   ]]> 
40   </SellerNick>
41   <Seller>wholetire</Seller> 
42   <SellerGrd>1</SellerGrd> 
43   <Rating>27</Rating> 
44 - <DetailPageUrl>
45 - <![CDATA[ http://www.11st.co.kr/product/SellerProductDetail.tmall?method=getSellerProductDetail&prdNo=168246154
46   ]]> 
47   </DetailPageUrl>
48   <SalePrice>71940</SalePrice> 
49 - <Delivery>
50 - <![CDATA[ 무료
51   ]]> 
52   </Delivery>
53   <ReviewCount>9</ReviewCount> 
54   <BuySatisfy>100</BuySatisfy> 
55   <MinorYn>Y</MinorYn> 
56 - <Benefit>
57   <Discount>18860</Discount> 
58   <Mileage>0</Mileage> 
59   </Benefit>
60   </Product>
|

댓글 1개

2012-06-04 (월) 10:16:47
아래소스는 제가 xml처리한 다른 방식입니다.
간추려서 수정을 했습니다.
참고가 되실길 바랍니다.

---------------------------------------------
ini_set('allow_url_fopen','ON') ;

$xml = @simplexml_load_file(주소);
$cnt = count($xml->Product);

for($i=0;$i<$cnt;$i++){
print $xml->Product[$i]->ProductName;
}
---------------------------------------------
http://php.net/manual/en/function.simplexml-load-file.php
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,188건
+
제목 글쓴이 날짜 조회
12-04-10 조회 3,804
12-04-06 조회 5,258
12-04-06 조회 3,025
12-04-04 조회 4,107
12-04-02 조회 3,465
12-03-30 조회 3,391
12-03-29 조회 5,867
12-03-28 조회 3,294
12-03-28 조회 4,198
12-03-27 조회 4,411
12-03-23 조회 3,693
12-03-21 조회 3,408
12-03-20 조회 3,206
12-03-18 조회 3,032
12-03-15 조회 3,205
12-03-12 조회 3,245
12-03-12 조회 3,227
12-03-08 조회 3,514
12-03-03 조회 3,558
12-03-03 조회 3,191
12-02-29 조회 3,387
12-02-28 조회 3,482
12-02-28 조회 3,296
12-02-28 조회 3,402
12-02-27 조회 3,817
12-02-27 조회 3,194
12-02-24 조회 3,405
12-02-24 조회 3,225
12-02-23 조회 3,630
12-02-20 조회 3,505
12-02-20 조회 4,002
12-02-17 조회 3,967
12-02-16 조회 3,073
12-02-15 조회 3,345
12-02-15 조회 3,490
12-02-14 조회 3,391
12-02-08 조회 3,398
12-02-06 조회 3,373
12-02-06 조회 3,818
12-01-25 조회 4,071
12-01-21 조회 3,967
12-01-20 조회 6,091
12-01-15 조회 3,582
12-01-15 조회 3,545
12-01-14 조회 3,443
12-01-14 조회 3,582
12-01-14 조회 3,310
12-01-12 조회 3,916
12-01-12 조회 4,194
12-01-10 조회 3,521
12-01-10 조회 4,318
12-01-09 조회 3,680
12-01-08 조회 6,944
12-01-08 조회 3,153
12-01-07 조회 3,503
12-01-06 조회 3,519
12-01-04 조회 3,658
12-01-03 조회 3,520
12-01-02 조회 3,919
12-01-02 조회 3,109
12-01-01 조회 4,032
12-01-01 조회 3,380
11-12-31 조회 3,624
11-12-31 조회 3,738
11-12-31 조회 3,569
11-12-30 조회 3,793
11-12-30 조회 4,980
11-12-30 조회 4,097
11-12-30 조회 4,252
11-12-30 조회 3,728
11-12-30 조회 3,443
11-12-30 조회 3,406
11-12-29 조회 3,175
11-12-22 조회 3,369
11-12-21 조회 3,269
11-12-20 조회 3,589
11-12-15 조회 5,364
11-12-15 조회 4,381
11-12-14 조회 5,404
11-12-12 조회 3,587
11-12-03 조회 3,353
11-11-26 조회 3,385
11-11-19 조회 3,662
11-11-01 조회 2,961
11-10-23 조회 2,626
11-10-21 조회 2,597
11-10-15 조회 3,114
11-10-15 조회 2,848
11-10-15 조회 3,519
11-10-15 조회 2,740
11-10-14 조회 2,779
11-10-13 조회 2,555
11-10-11 조회 2,802
11-10-10 조회 2,623
11-09-30 조회 3,482
11-09-30 조회 2,896
11-09-29 조회 4,703
11-09-29 조회 2,599
11-09-23 조회 2,620
11-09-16 조회 2,961