Xml 将WSResponse解析为对象

Xml 将WSResponse解析为对象,xml,playframework,Xml,Playframework,我当前正在从一个SOAP调用接收WSResponse,我正在尝试解析该响应。下面是反应的样子 <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/

我当前正在从一个SOAP调用接收WSResponse,我正在尝试解析该响应。下面是反应的样子

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <ns0:HelpDesk_QueryList_ServiceResponse xmlns:ns0="urn:HPD_IncidentInterface_WS">
            <ns0:getListValues>
                <ns0:Assigned_Group>Operations Tier 1</ns0:Assigned_Group>
                <ns0:Assigned_Group_Shift_Name />
                <ns0:Assigned_Support_Company>HTEN</ns0:Assigned_Support_Company>
                <ns0:Assigned_Support_Organization>Operations</ns0:Assigned_Support_Organization>
                <ns0:Assignee>App Admin</ns0:Assignee>
                <ns0:Categorization_Tier_1>Equipment</ns0:Categorization_Tier_1>
                <ns0:Categorization_Tier_2 />
                <ns0:Categorization_Tier_3 />
                <ns0:City>FakeCity</ns0:City>
                <ns0:Closure_Manufacturer />
                <ns0:Closure_Product_Category_Tier1 />
                <ns0:Closure_Product_Category_Tier2 />
                <ns0:Closure_Product_Category_Tier3 />
                <ns0:Closure_Product_Model_Version />
                <ns0:Closure_Product_Name />
                <ns0:Company>FakeCompany</ns0:Company>
                <ns0:Contact_Company>FakeCompany</ns0:Contact_Company>
                <ns0:Contact_Sensitivity>Standard</ns0:Contact_Sensitivity>
                <ns0:Country>United States</ns0:Country>
                <ns0:Department />
                <ns0:Summary>HTEN-FakeCompany-0001</ns0:Summary>
                <ns0:Notes>----Event Information-------
    Event: highCPU
    HostName: HTEN-FakeCompany-0001
    Severity: CRITICAL
    Event Time: 4/10/2014 13:23:00
    Message: highCPU HTEN-FakeCompany-0001 CPU has been greater than 75% for the last 15 minutes

    -----Event Cat Information-----
    Device Type: 
    Company: FakeCompany
    Ticket Type: 
    Service Impact: 
    Wiki Article:  

    --Assigned to the Following Group--</ns0:Notes>
                <ns0:First_Name>FakeCompany</ns0:First_Name>
                <ns0:Impact>2-Significant/Large</ns0:Impact>
                <ns0:Incident_Number>INC000000057006</ns0:Incident_Number>
                <ns0:Internet_E-mail />
                <ns0:Last_Name>Event</ns0:Last_Name>
                <ns0:Manufacturer />
                <ns0:Middle_Initial />
                <ns0:Organization />
                <ns0:Phone_Number>###</ns0:Phone_Number>
                <ns0:Priority>High</ns0:Priority>
                <ns0:Priority_Weight>20</ns0:Priority_Weight>
                <ns0:Product_Categorization_Tier_1 />
                <ns0:Product_Categorization_Tier_2 />
                <ns0:Product_Categorization_Tier_3 />
                <ns0:Product_Model_Version />
                <ns0:Product_Name />
                <ns0:Region>North America</ns0:Region>
                <ns0:Reported_Source>Other</ns0:Reported_Source>
                <ns0:Resolution />
                <ns0:Resolution_Category>LEC</ns0:Resolution_Category>
                <ns0:Resolution_Category_Tier_2>L3</ns0:Resolution_Category_Tier_2>
                <ns0:Resolution_Category_Tier_3>CABLE CUT</ns0:Resolution_Category_Tier_3>
                <ns0:Service_Type>Infrastructure Event</ns0:Service_Type>
                <ns0:Site>FakeCity NOCP</ns0:Site>
                <ns0:Site_Group>North America</ns0:Site_Group>
                <ns0:Status>Closed</ns0:Status>
                <ns0:Status_Reason />
                <ns0:Urgency>2-High</ns0:Urgency>
                <ns0:VIP>No</ns0:VIP>
                <ns0:ServiceCI />
                <ns0:ServiceCI_ReconID />
                <ns0:HPD_CI>HTEN-FakeCompany-0001</ns0:HPD_CI>
                <ns0:HPD_CI_ReconID>RE005056A12C40QdJGUwmzNHAQLroA</ns0:HPD_CI_ReconID>
                <ns0:HPD_CI_FormName>BMC_BUSINESSSERVICE</ns0:HPD_CI_FormName>
                <ns0:z1D_CI_FormName>AST:BusinessService</ns0:z1D_CI_FormName>
            </ns0:getListValues>
            <ns0:getListValues>
                <ns0:Assigned_Group>Operations Tier 1</ns0:Assigned_Group>
                <ns0:Assigned_Group_Shift_Name />
                <ns0:Assigned_Support_Company>HTEN</ns0:Assigned_Support_Company>
                <ns0:Assigned_Support_Organization>Operations</ns0:Assigned_Support_Organization>
                <ns0:Assignee>FakeCompany Customer</ns0:Assignee>
                <ns0:Categorization_Tier_1 />
                <ns0:Categorization_Tier_2 />
                <ns0:Categorization_Tier_3 />
                <ns0:City>FakeCity</ns0:City>
                <ns0:Closure_Manufacturer />
                <ns0:Closure_Product_Category_Tier1 />
                <ns0:Closure_Product_Category_Tier2 />
                <ns0:Closure_Product_Category_Tier3 />
                <ns0:Closure_Product_Model_Version />
                <ns0:Closure_Product_Name />
                <ns0:Company>FakeCompany</ns0:Company>
                <ns0:Contact_Company>FakeCompany</ns0:Contact_Company>
                <ns0:Contact_Sensitivity>Standard</ns0:Contact_Sensitivity>
                <ns0:Country>United States</ns0:Country>
                <ns0:Department />
                <ns0:Summary>na</ns0:Summary>
                <ns0:Notes />
                <ns0:First_Name>FakeCompany</ns0:First_Name>
                <ns0:Impact>3-Moderate/Limited</ns0:Impact>
                <ns0:Incident_Number>INC000000057016</ns0:Incident_Number>
                <ns0:Internet_E-mail />
                <ns0:Last_Name>Event</ns0:Last_Name>
                <ns0:Manufacturer />
                <ns0:Middle_Initial />
                <ns0:Organization />
                <ns0:Phone_Number>###</ns0:Phone_Number>
                <ns0:Priority>Medium</ns0:Priority>
                <ns0:Priority_Weight />
                <ns0:Product_Categorization_Tier_1 />
                <ns0:Product_Categorization_Tier_2 />
                <ns0:Product_Categorization_Tier_3 />
                <ns0:Product_Model_Version />
                <ns0:Product_Name />
                <ns0:Region>North America</ns0:Region>
                <ns0:Reported_Source>Other</ns0:Reported_Source>
                <ns0:Resolution />
                <ns0:Resolution_Category>NONE</ns0:Resolution_Category>
                <ns0:Resolution_Category_Tier_2>FALSE OUTAGE</ns0:Resolution_Category_Tier_2>
                <ns0:Resolution_Category_Tier_3>FALSE OUTAGE</ns0:Resolution_Category_Tier_3>
                <ns0:Service_Type>Infrastructure Event</ns0:Service_Type>
                <ns0:Site>FakeCity NOCP</ns0:Site>
                <ns0:Site_Group>North America</ns0:Site_Group>
                <ns0:Status>Closed</ns0:Status>
                <ns0:Status_Reason />
                <ns0:Urgency>3-Medium</ns0:Urgency>
                <ns0:VIP>No</ns0:VIP>
                <ns0:ServiceCI />
                <ns0:ServiceCI_ReconID />
                <ns0:HPD_CI />
                <ns0:HPD_CI_ReconID />
                <ns0:HPD_CI_FormName />
                <ns0:z1D_CI_FormName />
            </ns0:getListValues>
        </ns0:HelpDesk_QueryList_ServiceResponse>
    </soapenv:Body>
</soapenv:Envelope>

一级运营
列支敦士登
操作
应用管理
装备
伪造
假货公司
假货公司
标准
美国
列支敦士登-FakeCompany-0001
----事件信息-------
事件:高CPU
主机名:shen-FakeCompany-0001
严重程度:严重
活动时间:2014年10月4日13:23:00
消息:highCPU-FakeCompany-0001 CPU在过去15分钟内已超过75%
-----事件Cat信息-----
设备类型:
公司:伪造公司
票证类型:
服务影响:
维基文章:
--分配给以下组--
假货公司
2-重要/大型
INC000000057006
事件
###
高
20
北美
其他
莱克
L3
电缆切割
基础设施活动
伪造城市NOCP
北美
关闭
2高
不
列支敦士登-FakeCompany-0001
RE005056A12C40QdJGUwmzNHAQLroA
BMC_商务服务
AST:商务服务
一级运营
列支敦士登
操作
伪造公司客户
伪造
假货公司
假货公司
标准
美国
na
假货公司
3-中等/有限
INC000000057016
事件
###
中等
北美
其他
没有一个
错误停机
错误停机
基础设施活动
伪造城市NOCP
北美
关闭
三中
不
我不需要所有的元素,但我确实需要的元素我想放在POJO中。到目前为止,有没有更好的方法来处理我正在做的事情?这就是我所做的

WSResponse wsresponse = request.post(soapRequest).get(TIMEOUT);
Document response = wsresponse.asXml();
List < TroubleTicket > tickets = Lists.newArrayList();
Node node = response.getElementsByTagNameNS("*", QUERY_LIST_RESPONSE).item(0);
NodeList children = node.getChildNodes();
Logger.info("Parsing tickets from response, found " + children.getLength() + " tickets.");
for (int counter = 0; counter < children.getLength(); counter++) {
    NodeList grandChildren = children.item(counter).getChildNodes();
    for (int childCounter = 0; childCounter < grandChildren.getLength(); childCounter++) {
        TroubleTicket ticket = new TroubleTicket();
        String element = grandChildren.item(childCounter).getNodeName();
        String elementText = grandChildren.item(childCounter).getTextContent();
        switch (element) {
            case "Summary":
                ticket.setSummary(elementText);
                break;
            case "Notes":
                ticket.setNotes(elementText);
                break;
        }
    }
}
WSResponse-WSResponse=request.post(soapRequest.get)(超时);
Document response=wsresponse.asXml();
Listtickets=Lists.newArrayList();
Node Node=response.getElementsByTagnames(“*”,查询\u列表\u响应)。项(0);
NodeList childrends=node.getChildNodes();
info(“从响应中解析票据,找到“+children.getLength()+”票据”);
对于(int counter=0;counter
继上,我还建议使用Scala来解组XML以对对象建模。当您使用Play框架时,这是一个非常面向Scala的框架,至少可以探索使用Scala处理XML的选项

这是Scala中XML处理的一个非常粗略的例子。即使您不熟悉Scala,希望您能看到处理是基于使用类似XPath的选择器,并且代码比使用Java编写文档API简洁得多

Web服务调用 HelpDeskServiceResponse.scala HelpDeskTicket.scala
我最后做的是获取WSResponse并调用response.asXml();这返回了一个XML文档,我循环该文档以查找保存在字符串列表中所需的字段。然后,我构建了Json对象,在循环响应时将其返回给客户机

Node node = response.getElementsByTagNameNS("*", elementTagName).item(0);
NodeList children = node.getChildNodes();
ArrayNode responseTickets = Json.newObject().arrayNode();

for (int counter = 0; counter < children.getLength(); counter++) {
ObjectNode ticketNode = getTicketFromResponse(children.item(counter).getChildNodes(), responseFields);
if (ticketNode.size() != 0) {
   responseTickets.add(ticketNode);
   }
}
return responseTickets;
Node Node=response.getElementsByTagnames(“*”,elementTagName).item(0);
NodeList childrends=node.getChildNodes();
ArrayNode ResponseCkets=Json.newObject().ArrayNode();
对于(int counter=0;counter
getTicketsFromResponse方法:

ObjectNode ticketNode = Json.newObject();
    for (int childCounter = 0; childCounter < nodeList.getLength(); childCounter++) {
        String element = nodeList.item(childCounter).getNodeName();
        String elementText = nodeList.item(childCounter).getTextContent();
        element = element.substring(4, element.length());
        if (responseFields.contains(element)) {
            ticketNode.put(element, elementText);
            if (SUMMARY_FIELD.equals(element) && !Strings.isEmpty(elementText)) {
                // Retrieve the id of the resource
                try {
                    Long id = null;
                    Matcher matcher = Pattern.compile(SERVICE_ID_PATTERN).matcher(elementText);
                    if (matcher.find()) {
                        String matched = matcher.group(0);
                        Matcher numberMatcher = Pattern.compile("\\d+").matcher(matched);
                        if (numberMatcher.find()) {
                            id = Long.parseLong(numberMatcher.group(0));
                            Service service = Service.findByIdNotNull(id);
                            ticketNode.put(SERVICE_NAME_FIELD,
                                    String.format(SERVICE_NAME_FORMAT, service.getName(), service.getUSI()));
                        }
                    }
                    ticketNode.put(SERVICE_ID_FIELD, id);
                } catch (NullPointerException exception) {
                    Logger.error("Unable to get the service for the configuration item", exception);
                } catch (NumberFormatException exception) {
                    Logger.error("Unable to parse the id from the summary field. ");
                } catch (ResourceNotFoundException exception) {
                    Logger.error("Unable to get the service.", exception);
                }
            } else if (SUBMIT_DATE_FIELD.equals(element) && !Strings.isEmpty(elementText)) {
                try {
                    Date submitDate = new SimpleDateFormat(RESPONSE_DATE_FORMAT).parse(elementText);
                    elementText = new SimpleDateFormat(SKYLIGHT_DATE_FORMAT).format(submitDate);
                    ticketNode.put(element, elementText);
                } catch (ParseException exception) {
                    Logger.error("Unable to parse the submit date. ", exception);
                }
            }
        }
    }
    return ticketNode;
ObjectNode ticketNode=Json.newObject();
对于(int childCounter=0;childCounterimport scala.xml.Node

class HelpDeskTicket(node : Node) {

  lazy val summary = node \ "Summary"

  lazy val notes = node \ "Notes"

}
Node node = response.getElementsByTagNameNS("*", elementTagName).item(0);
NodeList children = node.getChildNodes();
ArrayNode responseTickets = Json.newObject().arrayNode();

for (int counter = 0; counter < children.getLength(); counter++) {
ObjectNode ticketNode = getTicketFromResponse(children.item(counter).getChildNodes(), responseFields);
if (ticketNode.size() != 0) {
   responseTickets.add(ticketNode);
   }
}
return responseTickets;
ObjectNode ticketNode = Json.newObject();
    for (int childCounter = 0; childCounter < nodeList.getLength(); childCounter++) {
        String element = nodeList.item(childCounter).getNodeName();
        String elementText = nodeList.item(childCounter).getTextContent();
        element = element.substring(4, element.length());
        if (responseFields.contains(element)) {
            ticketNode.put(element, elementText);
            if (SUMMARY_FIELD.equals(element) && !Strings.isEmpty(elementText)) {
                // Retrieve the id of the resource
                try {
                    Long id = null;
                    Matcher matcher = Pattern.compile(SERVICE_ID_PATTERN).matcher(elementText);
                    if (matcher.find()) {
                        String matched = matcher.group(0);
                        Matcher numberMatcher = Pattern.compile("\\d+").matcher(matched);
                        if (numberMatcher.find()) {
                            id = Long.parseLong(numberMatcher.group(0));
                            Service service = Service.findByIdNotNull(id);
                            ticketNode.put(SERVICE_NAME_FIELD,
                                    String.format(SERVICE_NAME_FORMAT, service.getName(), service.getUSI()));
                        }
                    }
                    ticketNode.put(SERVICE_ID_FIELD, id);
                } catch (NullPointerException exception) {
                    Logger.error("Unable to get the service for the configuration item", exception);
                } catch (NumberFormatException exception) {
                    Logger.error("Unable to parse the id from the summary field. ");
                } catch (ResourceNotFoundException exception) {
                    Logger.error("Unable to get the service.", exception);
                }
            } else if (SUBMIT_DATE_FIELD.equals(element) && !Strings.isEmpty(elementText)) {
                try {
                    Date submitDate = new SimpleDateFormat(RESPONSE_DATE_FORMAT).parse(elementText);
                    elementText = new SimpleDateFormat(SKYLIGHT_DATE_FORMAT).format(submitDate);
                    ticketNode.put(element, elementText);
                } catch (ParseException exception) {
                    Logger.error("Unable to parse the submit date. ", exception);
                }
            }
        }
    }
    return ticketNode;