2012-09-26 188 views
1

我有一个保存在字符串中的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:并希望将结果保存在String中。我知道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