在spring webservice response中设置xml响应头。

问题描述 投票:0回答:1

我想在spring webservice响应中返回这种类型的头,有人能帮帮我吗?

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <PullURIResponse xmlns:ns2="http://tempuri.org/"> <ResponseStatus Status="1" ts="2016-01-11T14:44:48+05:30" txn="123456789">1</ResponseStatus>//1-Success //0-Failure //9-Pending <DocDetails> <DocType>INCER</DocType>
</DocDetails></PullURIResponse>
java xml xml-parsing
1个回答
0
投票

/ a但繁琐但不需要任何额外的库。

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;

public class XMLTextExtractor {
    static String  val;

    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(val.getBytes()));
        XPath xpath = XPathFactory.newInstance().newXPath();

        XPathExpression expr = xpath.compile("//text()");
        NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        String textResult = "";

        for (int i = 0; i < nodes.getLength(); i++) {
            textResult += nodes.item(i).getNodeValue().trim().replaceAll("\\R+","");
        }

        System.out.println(textResult);
    }

    static {
        val = "<?xml version=\"1.0\"?><NGOSearchDocumentExt_Input> \n" +
                "    <Option>NGOSearchDocumentExt</Option><CabinetName>INOMNUTDB</CabinetName> \n" +
                "    <UserDBId>468315129</UserDBId><SearchText></SearchText><Rank>Y</Rank> \n" +
                "    <SearchOnPreviousVersions>N</SearchOnPreviousVersions> \n" +
                "    <LookInFolder>0</LookInFolder><IncludeSubFolder>Y</IncludeSubFolder> \n" +
                "    <Name></Name><Owner></Owner><CreationDateRange></CreationDateRange> \n" +
                "    <ExpiryDateRange></ExpiryDateRange><AccessedDateRange> \n" +
                "    </AccessedDateRange><RevisedDateRange></RevisedDateRange> \n" +
                "    <DataDefCriterion><DataDefCriteria><DataDefIndex>13</DataDefIndex> \n" +
                "    <IndexId>103</IndexId><Operator></Operator><IndexValue>12</IndexValue> \n" +
                "    <JoinCondition>AND</JoinCondition></DataDefCriteria>\n" +
                "    <DataDefCriteria><DataDefIndex>13</DataDefIndex><IndexId>103</IndexId> \n" +
                "    <Operator>like</Operator><IndexValue>abc</IndexValue> \n" +
                "    <JoinCondition>AND</JoinCondition></DataDefCriteria></DataDefCriterion> \n" +
                "    <SearchScope>0</SearchScope><PreviousList></PreviousList> \n" +
                "    <SearchOnAlias>N</SearchOnAlias><Keywords></Keywords> \n" +
                "    <GlobalIndexCriterion></GlobalIndexCriterion> \n" +
                "    <ReferenceFlag>O</ReferenceFlag><SortOrder>D</SortOrder> \n" +
                "    <GroupIndex>0</GroupIndex><StartFrom>1</StartFrom> \n" +
                "    <NoOfRecordsToFetch>10</NoOfRecordsToFetch><OrderBy>5</OrderBy><CheckOutStatus></CheckOutStatus><MaximumHitCountFlag>Y</MaximumHitCountFlag><CheckOutByUser></CheckOutByUser><ObjectTypes><ObjectType>1</ObjectType><ObjectType>2</ObjectType><ObjectType>8</ObjectType><ObjectType>11</ObjectType><ObjectType>13</ObjectType><ObjectType>14</ObjectType><ObjectType>15</ObjectType><ObjectType>16</ObjectType><ObjectType>17</ObjectType><ObjectType>20</ObjectType></ObjectTypes><DataAlsoFlag>Y</DataAlsoFlag><CreatedByAppName></CreatedByAppName><Author></Author><AnnotationFlag>Y</AnnotationFlag><LinkDocFlag>Y</LinkDocFlag><PrevDocIndex>0</PrevDocIndex><IncludeSystemFolder>NN</IncludeSystemFolder><IncludeTrashFlag>N</IncludeTrashFlag><ThesaurusFlag>N</ThesaurusFlag><RecordAlsoFlag>N</RecordAlsoFlag></NGOSearchDocumentExt_Input>";
    }
}

0
投票

类似于Kaira的回答,但重点是IndexValue元素。

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(xmlString));
Document document = documentBuilder.parse(inputSource);
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression expression = xpath.compile("//IndexValue/text()");
NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);

for (int n = 0; n < nodeList.getLength(); n++) {
    System.out.println(n + ": " + nodeList.item(n).getNodeValue());
}

0
投票

如果你沉迷于短代码,那么它可能更合适(但不是我的风格:-) )

public class XMLTextExtractor {
    static String  val;

    public static void main(String[] args) {

        String textResult = "";
        for (String fragment: val.replace("<?xml version=\"1.0\"?>", "").split(">")){
            textResult += fragment.replaceAll("<\\/.*|<.*","").trim();
        }
        System.out.println(textResult);
    }

    static {
        val = "<?xml version=\"1.0\"?><NGOSearchDocumentExt_Input> \n" +
                "    <Option>NGOSearchDocumentExt</Option><CabinetName>INOMNUTDB</CabinetName> \n" +
                "    <UserDBId>468315129</UserDBId><SearchText></SearchText><Rank>Y</Rank> \n" +
                "    <SearchOnPreviousVersions>N</SearchOnPreviousVersions> \n" +
                "    <LookInFolder>0</LookInFolder><IncludeSubFolder>Y</IncludeSubFolder> \n" +
                "    <Name></Name><Owner></Owner><CreationDateRange></CreationDateRange> \n" +
                "    <ExpiryDateRange></ExpiryDateRange><AccessedDateRange> \n" +
                "    </AccessedDateRange><RevisedDateRange></RevisedDateRange> \n" +
                "    <DataDefCriterion><DataDefCriteria><DataDefIndex>13</DataDefIndex> \n" +
                "    <IndexId>103</IndexId><Operator></Operator><IndexValue>12</IndexValue> \n" +
                "    <JoinCondition>AND</JoinCondition></DataDefCriteria>\n" +
                "    <DataDefCriteria><DataDefIndex>13</DataDefIndex><IndexId>103</IndexId> \n" +
                "    <Operator>like</Operator><IndexValue>abc</IndexValue> \n" +
                "    <JoinCondition>AND</JoinCondition></DataDefCriteria></DataDefCriterion> \n" +
                "    <SearchScope>0</SearchScope><PreviousList></PreviousList> \n" +
                "    <SearchOnAlias>N</SearchOnAlias><Keywords></Keywords> \n" +
                "    <GlobalIndexCriterion></GlobalIndexCriterion> \n" +
                "    <ReferenceFlag>O</ReferenceFlag><SortOrder>D</SortOrder> \n" +
                "    <GroupIndex>0</GroupIndex><StartFrom>1</StartFrom> \n" +
                "    <NoOfRecordsToFetch>10</NoOfRecordsToFetch><OrderBy>5</OrderBy><CheckOutStatus></CheckOutStatus><MaximumHitCountFlag>Y</MaximumHitCountFlag><CheckOutByUser></CheckOutByUser><ObjectTypes><ObjectType>1</ObjectType><ObjectType>2</ObjectType><ObjectType>8</ObjectType><ObjectType>11</ObjectType><ObjectType>13</ObjectType><ObjectType>14</ObjectType><ObjectType>15</ObjectType><ObjectType>16</ObjectType><ObjectType>17</ObjectType><ObjectType>20</ObjectType></ObjectTypes><DataAlsoFlag>Y</DataAlsoFlag><CreatedByAppName></CreatedByAppName><Author></Author><AnnotationFlag>Y</AnnotationFlag><LinkDocFlag>Y</LinkDocFlag><PrevDocIndex>0</PrevDocIndex><IncludeSystemFolder>NN</IncludeSystemFolder><IncludeTrashFlag>N</IncludeTrashFlag><ThesaurusFlag>N</ThesaurusFlag><RecordAlsoFlag>N</RecordAlsoFlag></NGOSearchDocumentExt_Input>";
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.