2012-05-15 5 views
4

큰 XML 파일이 있습니다. 모든 태그를 제거하고 노드 값만 남겨두고 싶습니다. 나는 각 노드 값을 별도의 라인에 넣고 싶다. 어떻게해야합니까?XML 파일에서 모든 태그 제거하기

자유 소프트웨어를 사용하거나 PHP 또는 ASP.NET 코드를 사용할 수 있습니까? 나는 또한 XSLT 옵션을 보았다. RegEX의 경우 너무 많습니다. 나는 PHP 옵션을 simplexml_load_file(), strip_tags(), get_file_contents() 보았지만 실패했다.

<?xml version="1.0" encoding="ISO-8859-1"?> 
<!-- a comment --> 
<catalog> 
    <cd> 
     <title>Empire Burlesque</title> 
     <artist>Bob Dylan</artist> 
     <country>USA</country> 
     <company>Columbia</company> 
     <price>10.90</price> 
       <address> 
         <city>Melbourne </city> 
         <zip>01803 </zip> 
       </address> 
     <year>1985</year> 
    </cd> 
    <cd> 
     <title>Hide your heart</title> 
     <artist>Bonnie Tyler</artist> 
     <country>UK</country> 
     <company>CBS Records</company> 
     <price>9.90</price> 
     <year>1988</year> 
    </cd> 

</catalog> 

편집 :이 내가 다른 것들 중, 시도 것입니다.

<?php 

$xml = simplexml_load_file('myxml.xml'); 
echo strip_tags($xml); 

?> 
+0

'strip_tags 0 보니 타일러
영국
CBS 기록()'작동합니다. 어떻게 사용하려고했는지 게시 할 수 있습니까? –

+0

이 질문에 대한 생각 >> sortof 태그를 구문 분석하는 방법에 대한 다른 질문으로 연결 http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags - 모두 조심해야한다고. – Kristian

+0

@ConnorPeet은'strip_tags'에 대한 코드 스 니펫을 추가했습니다. 나는 $ xml이 기본적으로 배열이기 때문에 어떤 결과물도 얻지 못한다. –

답변

5

이 나중에 수행해야합니다 당신이 줄 바꿈 누락 된

<?php 
$xml = file_get_contents('myxml.xml'); 
$xml = nl2br($xml); 
echo strip_tags($xml,"<br>"); 
?> 

이유 때문에 XML에 있었다, 그것은 일반 텍스트 줄 바꿈 \n HTML로 표시 할 때 당신이 있어야합니다 반면 명시 적 <br> 바꿈으로 저장됩니다. 이 때문에 좋은 PHP 사람들은 nl2br()이라는 편리한 함수를 만들었습니다. 여기

+0

BTW I 각 행을 조작 할 수있는 코드를 원합니다. 나는 노드 앞에 뭔가를 추가하고 후에 뭔가를 추가해야합니다. –

4

이 짧고 간단 XSLT 용액이다

<catalog> 
    <cd> 
     <title>Empire Burlesque</title> 
     <artist>Bob Dylan</artist> 
     <country>USA</country> 
     <company>Columbia</company> 
     <price>10.90</price> 
     <address> 
      <city>Melbourne </city> 
      <zip>01803 </zip> 
     </address> 
     <year>1985</year> 
    </cd> 
    <cd> 
     <title>Hide your heart</title> 
     <artist>Bonnie Tyler</artist> 
     <country>UK</country> 
     <company>CBS Records</company> 
     <price>9.90</price> 
     <year>1988</year> 
    </cd> 
</catalog> 
이 변환이 제공된 XML 문서 ( 아무 XML 문서 일 것)에 도포

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="text()"> 
    <br /><xsl:value-of select="concat(.,'&#xA;')"/> 
</xsl:template> 
</xsl:stylesheet> 

원하는 결과가 생성됩니다.

<br/>Empire Burlesque 
<br/>Bob Dylan 
<br/>USA 
<br/>Columbia 
<br/>10.90 
<br/>Melbourne 
<br/>01803 
<br/>1985 
<br/>Hide your heart 
<br/>Bonnie Tyler 
<br/>UK 
<br/>CBS Records 
<br/>9.90 
<br/>1988 

과 같이 브라우저에 표시됩니다


제국 소극이
밥 딜런이
미국
컬럼비아
10.90
멜버른
01,803
1985
숨기기 당신의 심장
9.90
1988

+0

감사합니다. 이 또한 나를 도울 것입니다. 그건 그렇고 아무것도 찾지 않고 그냥 태그를 벗기고 싶었습니다. –

+0

@Thecrocodilehunter : 천만에. –

관련 문제