2013-07-21 4 views
-2

일부 XML 태그 안에 텍스트를 반환하는 정규 표현식을 작성하려고합니다. 예를 들어이 형식의 파일이있는 경우정규식을 작성하여 XML 태그 안에 텍스트를 가져 오는 방법은 무엇입니까?

<name>Joe Blog</name> 
<email>[email protected]</email> 
<address>123 sample st</address> 

주소 입력란에 텍스트를 어떻게 추출합니까?

이 문제에 도움을 주시면 감사하겠습니다. 이 표현은 주소 값

<address>(.*?)<\/address>

enter image description here

을 캡처하고 첫 번째 캡처 그룹에 배치됩니다

+2

가 사용할 언어를 선택합니다. XML 코드를 쉽게 파싱 할 수있는 라이브러리를 사용하지 않는 이유는 무엇입니까? – nio

+2

같은 질문에 대한 위대한 답변을 읽어 보시기 바랍니다. http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – yonosoytu

+1

그냥 기다리고 있습니다. 누군가 XHTML 응답에 연결될 때까지 ... (편집 : 오, 잠깐, 거기 간다.) – acdcjunior

답변

2

덕분에,

샘플 텍스트

<name>Joe Blog</name> 
<email>[email protected]</email> 
<address>123 sample st</address> 

일치

[0][0] = <address>123 sample st</address> 
[0][1] = 123 sample st 

그러나

대부분의 langages를 사용하여 당신은 PHP에서이 작업을 수행 할 수 있습니다 예를 들어, HTML 구문 분석 도구가 :

$dom = new DOMDocument(); 
$dom->loadHTML($your_html_here); 
$addresses= $dom->getElementsByTagName('address'); 
foreach($addresses as $address) { 
    $address = $address->innertext; 
    // do something 
} 
+0

어떻게 정규 표현식의 멋진 그래프를 만들 수 있습니까? – nio

+0

나는 http://www.debuggex.com/에서 그것들을 만든다. 당신의 식을 붙여 넣기 만하면 다이어그램을 보여준다. –

0

당신을 수행 자신의 글을 써야하거나 tinyxml2를 사용할 수 있습니까? SAX 파서없이 tinyxml2를 사용하여 문서를 알고있는 경우

같은 것을 시도 :

/* ------ Example 2: Lookup information. ---- */  
{ 
    XMLDocument doc; 
    doc.LoadFile("dream.xml"); 

    // Structure of the XML file: 
    // - Element "PLAY"  the root Element, which is the 
    //      FirstChildElement of the Document 
    // - - Element "TITLE" child of the root PLAY Element 
    // - - - Text   child of the TITLE Element 

    // Navigate to the title, using the convenience function, 
    // with a dangerous lack of error checking. 
    const char* title = doc.FirstChildElement("PLAY")->FirstChildElement("TITLE")->GetText(); 
    printf("Name of play (1): %s\n", title); 

    // Text is just another Node to TinyXML-2. The more 
    // general way to get to the XMLText: 
    XMLText* textNode = doc.FirstChildElement("PLAY")->FirstChildElement("TITLE")->FirstChild()->ToText(); 
    title = textNode->Value(); 
    printf("Name of play (2): %s\n", title); 
} 

당신이 SAX 파서를 사용하려는 경우, tinyxml2 모드 그뿐만 아니라 지원합니다. 예를 들어, cocos2d-x로 넘어 가서 tinyxml2를 호출하고 서브 클래스 인 CCSAXParser 클래스를 살펴보면 거의 모든 XML 파일을 구문 분석 할 수 있습니다.

출처 : tinyXML2 cocos2d-x

관련 문제