2012-09-26 3 views
1

String에 저장 한 XML 문서가 있습니다. 문자열은 다음과 같습니다.문자열 배열에서 문자열을 찾아 추출합니다.

<?xml version=\"1.0\" encoding=\"http://schemas.xmlsoap.org/soap/envelope/\" standalone=\"no\"?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header xmlns:wsa=\"http://www.w3.org/2005/08/addressing\"><axis2:ServiceGroupId xmlns:axis2=\"http://ws.apache.org/namespaces/axis2\" wsa:IsReferenceParameter=\"true\">urn:uuid:2BC5F552AF3179755C1348038695049</axis2:ServiceGroupId><wsa:To>http://localhost:8081/axis2/services/TCAQSRBase</wsa:To><wsa:MessageID>urn:uuid:599362E68F35A38AFA1348038695733</wsa:MessageID><wsa:Action>http://www.transcat-plm.com/TCAQSRBase/TCAQSR_BAS_ServerGetOsVariable</wsa:Action></soapenv:Header><soapenv:Body><ns1:TCAQSR_BAS_ServerGetOsVariableInput xmlns:ns1=\"http://www.transcat-plm.com/TCAQSRBase/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"ns1:TCAQSR_BAS_ServerGetOsVariableInputType\"><ns1:TCAQSR_BAS_BaseServerGetInputKey>USERNAME</ns1:TCAQSR_BAS_BaseServerGetInputKey></ns1:TCAQSR_BAS_ServerGetOsVariableInput></soapenv:Body></soapenv:Envelope> 

문자열에 문자열이 어떻게 표시되는지 알지 못합니다.

그러나 나는 <axis2:ServiceGroupId xmlns:axis2="http://ws.apache.org/namespaces/axis2"></axis2:ServiceGroupId> 사이의 용어를 추출하고 싶습니다. 어느 것이 urn : uuid :이며 문자열에 결과를 저장하고 싶습니다. 나는 xpath를 알고 있지만, 제 경우에는 xpath를 사용할 수 없습니다.

정말 도움이 될 것입니다.

미리 감사.

+0

XML 파서는 어떻게됩니까? – sp00m

+0

@ sp00m 죄송합니다. 나는이 일에 대해 사전 지식이 없습니다. 그리고 XML 구문 분석기가 무엇인지 정확히 알지 못합니다. – Spaniard89

답변

2
int startPos = xmlString.indexOf("<axis2...>") + "<axis2...>".length(); 
int endPos = xmlString.indexOf("</value2...>"); 
String term = xmlString.substring(startPos,endPos); 

귀하의 질문에 대한 답변을 드리겠습니다. 한 줄로도 처리 할 수 ​​있습니다.

+0

''이미 표현식에 ""이 있기 때문에 삽입하는 데 문제가 있습니다. 다시 ""를 추가해야합니다. indexOf 용어로 오류가 발생했습니다. – Spaniard89

+0

당신은 당신의 "내부 문자열 변경"\ " –

+0

많은 친구 감사합니다. 그렇게하면 정말 빠르고 도움이됩니다. – Spaniard89

1

정규식을 사용하십시오. 이상한 정규 표현식으로 전체 XML 문자열을 구문 분석하면 <axis2:ServiceGroupId xmlns:axis2="http://ws.apache.org/namespaces/axis2">(.+?) </axis2:ServiceGroupId>과 같은 특정 문제를 해결할 수 있습니다.

내가 특정 문제에 대한 작성한 유용한 조각 :

String yourInput = "<wsa:ReferenceParameters><axis2:ServiceGroupId xmlns:axis2=\"http://ws.apache.org/namespaces/axis2\">urn:uuid:2BC5F552AF3179755C1348038695049</axis2:ServiceGroupId></wsa:ReferenceParameters>"; 
    Pattern pattern = Pattern 
      .compile("<axis2:ServiceGroupId xmlns:axis2=\"http://ws.apache.org/namespaces/axis2\">(.+?)</axis2:ServiceGroupId>"); 
    Matcher matcher = pattern 
      .matcher(yourInput); 
    matcher.find(); 
    System.out.println(matcher.group(1)); 

matcher.group (1) 문자열이 원하는 반환, 당신이 다른 변수에 할당하고 변수 등의 것을 사용할 수 있습니다

+0

어떻게 할 수 있습니까? 나는 이것에 대한 사전 지식이 없다. 어떤 도움이라도 대단히 감사 할 것입니다. – Spaniard89

+0

@Kishorepandey 내 게시물에 코드 스 니펫을 포함 시켰습니다. 도움이되기를 바랍니다. 그렇게한다면 upvote 받아 들여 :) 감사. – Juvanis

+0

내 입력 문자열은 어디에 있습니까? – Spaniard89

관련 문제