Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何删除soap头和附加标记以检索所需的xml?_Java_Xml_Soap_Xpath - Fatal编程技术网

Java 如何删除soap头和附加标记以检索所需的xml?

Java 如何删除soap头和附加标记以检索所需的xml?,java,xml,soap,xpath,Java,Xml,Soap,Xpath,这是我使用的字符串格式的xml <?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <ns:processRequestResponse xmlns:ns="http://service.soap.oneflexi.com"><ns:re

这是我使用的字符串格式的xml

<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:processRequestResponse xmlns:ns="http://service.soap.oneflexi.com"><ns:return>
<?xml version="1.0" encoding="UTF-8"?> 
<ItemRs language="SG" currency="SGD"> 
  <Items>
 <Item>
   <CategoryCode />
    <CategoryDescription />
   <ItemCode>356</ItemCode>
    <ItemDescription>20% offer, Latest model</ItemDescription>
   <Quantity />
    <UnitPrice>24560</UnitPrice>
    <ItemBigImagesURL>http://goo.gl/klCGG4</ItemBigImagesURL>
    <ItemContent>Front Loading Washing Machine 6.5Kg Capacity 6 Motion Direct Drive</ItemContent>
   <ErrorCode />
    <ErrorMessage />
    <Beaconid>2499</Beaconid>
  </Item>
  </Items>
 <MID />
 </ItemRs>
</ns:return>
</ns:processRequestResponse>
</soapenv:Body>
</soapenv:Envelope>

356
20%优惠,最新型号
24560
http://goo.gl/klCGG4
前装式洗衣机6.5Kg容量6运动直接驱动
2499
我需要的输出应该是上述xml的子集,删除soap头和其他标记。如何在java中实现此输出

<ItemRs language="SG" currency="SGD"> 
  <Items>
 <Item>
   <CategoryCode />
    <CategoryDescription />
   <ItemCode>356</ItemCode>
    <ItemDescription>20% offer, Latest model</ItemDescription>
   <Quantity />
    <UnitPrice>24560</UnitPrice>
    <ItemBigImagesURL>http://goo.gl/klCGG4</ItemBigImagesURL>
    <ItemContent>Front Loading Washing Machine 6.5Kg Capacity 6 Motion Direct Drive</ItemContent>
   <ErrorCode />
    <ErrorMessage />
    <Beaconid>2499</Beaconid>
  </Item>
  </Items>
 <MID />
 </ItemRs>

356
20%优惠,最新型号
24560
http://goo.gl/klCGG4
前装式洗衣机6.5Kg容量6运动直接驱动
2499
如果您有任何帮助,我们将不胜感激

我已尝试使用此线程中给出的示例:[1]:

下面是java代码

public static void main(String... args)
    throws Exception
    {
//      String xml = "<A><B><id>0</id></B><B><id>1</id></B></A>";
        String xml =    "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.soap.oneflexi.com\"><soapenv:Header/><soapenv:Body><ser:processRequest><ser:in0><![CDATA[<BconRq language=\"SG\" currency=\"SGD\"><RqHeader><Date>20140614</Date><Time>162944</Time><TimeZone>GMT+06:00</TimeZone><MessageType>5320</MessageType><VersionNo>9005001</VersionNo><AppVersion>5.0.1</AppVersion><MerchantId>2003001</MerchantId><TerminalId>2004002</TerminalId><StanNo/><LastTxnRefNo/><OTP/></RqHeader><OfferMerchant/><OfferFlag>Y</OfferFlag><BconId>1234</BconId><Items><Item><ItemCode>VC01</ItemCode></Item></Items></BconRq>]]></ser:in0><ser:in4>?</ser:in4></ser:processRequest></soapenv:Body></soapenv:Envelope>";

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));

        XPath xPath = XPathFactory.newInstance().newXPath();
        Node result = (Node)xPath.evaluate(" Soapenv:Envelope/Soapenv:Body/ns:processRequestResponse/ns:return/BconRs[language=\"SG\"]", doc, XPathConstants.NODE);

//      Node result = (Node)xPath.evaluate("A/B[id = '1']", doc, XPathConstants.NODE);
//  Soapenv:Envelope/Soapenv:Body/ns:processRequestResponse/ns:return/BconRs[language="SG"]
        System.out.println(nodeToString(result));
    }

    private static String nodeToString(Node node)
    throws TransformerException
    {
        StringWriter buf = new StringWriter();
        Transformer xform = TransformerFactory.newInstance().newTransformer();
        xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        xform.transform(new DOMSource(node), new StreamResult(buf));
        return(buf.toString());
    }
publicstaticvoidmain(字符串…参数)
抛出异常
{
//字符串xml=“01”;
字符串xml=“20140614162944GMT+06:00532090050015.0.120030012004002Y1234VC01]>?”;
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
Document doc=dbf.newDocumentBuilder().parse(新的InputSource(新的StringReader(xml));
XPath=XPathFactory.newInstance().newXPath();
Node result=(Node)xPath.evaluate(“Soapenv:Envelope/Soapenv:Body/ns:processRequestResponse/ns:return/BconRs[language=\“SG\”],doc,XPathConstants.Node);
//节点结果=(节点)xPath.evaluate(“A/B[id='1']”,doc,XPathConstants.Node);
//Soapenv:Envelope/Soapenv:Body/ns:processRequestResponse/ns:return/BconRs[language=“SG”]
System.out.println(nodeToString(result));
}
私有静态字符串nodeToString(节点节点)
抛出TransformerException
{
StringWriter buf=新StringWriter();
Transformer xform=TransformerFactory.newInstance().newTransformer();
setOutputProperty(OutputKeys.OMIT_XML_声明,“yes”);
transform(新的DOMSource(节点),新的StreamResult(buf));
返回(buf.toString());
}

我可能认为XPATH遍历节点ItemRs

时可能有错误,正如您也怀疑的那样,XPATH看起来不正确。您可能希望尝试以下简单的XPath:

//ItemRs[@language='SG']

要在XPath中引用属性,需要在属性名称的开头使用
@
。我尽量避免在XPath中提到名称空间前缀,因为通常我们需要以某种方式注册名称空间前缀到名称空间URI的映射(我不知道如何在Java中做到这一点,我更喜欢.NET).

向我们展示您尝试过的Java代码。您可以尝试使用XML解析器或在最后一种情况下使用regex@Tichodroma:我已经添加了我尝试过的代码。