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。

并且非常感谢任何帮助。

提前非常感谢。

4

2 回答 2

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

我希望我能正确回答你的问题。您也可以在一行中完成。

于 2012-09-26T11:18:44.893 回答
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)返回所需的字符串,您可以将其分配给另一个变量并使用该变量等。

于 2012-09-26T11:13:42.567 回答