2017-12-24 36 views
0

저는 XML을 분할하여 activemq로 보내고 header orderid와 region을 기록하려고합니다. 지역을 얻기 위해 나는 자바 방법을 사용하고 있습니다 :XPATH를 사용하여 헤더를 설정할 수 없습니다

 import org.apache.camel.language.NamespacePrefix; 
     import org.apache.camel.language.XPath; 

     /** 
     * This class contains business logic that determines the region for a country. It is used by the Camel route in this example. 
     */ 
     public class RegionSupport { 

      public static final String AMER = "AMER"; 
      public static final String APAC = "APAC"; 
      public static final String EMEA = "EMEA"; 

      /** 
      * Get the region code that corresponds to the given country code. 
      * 
      * This method can be used as a plain Java method. However, when it is used inside a Camel route, the @XPath annotation will 
      * evaluate the XPath expression and use the result as the method parameter. In this case, it will fetch the country code 
      * from the order XML message. So, the method will determine the region code for the country that is in the XML message. 
      * 
      * @param country the country code 
      * @return the region code 
      */ 
      public String getRegion(@XPath(value = "/order:order/order:customer/order:country", 


    String country) { 
       if (country.equals("AU")) { 
        return APAC; 
       } else if (country.equals("US")) { 
        return AMER; 
       } else { 
        return EMEA; 
       } 
      } 
     } 

XML 입력 : I는 Exchange에서 ID와 헤더 = 2012_0001 지역 = APAC를 기록해야 XML을 아래에 따라 그래서

<?xml version="1.0" encoding="UTF-8"?> 
<orders> 
    <order id="2012_0001"> 

     <customer id="A0001"> 
      <name>Antwerp Zoo</name> 
      <city>Antwerp</city> 
      <country>AU</country> 
     </customer> 

     <date>2012-03-01</date> 

     <orderlines> 
      <orderline> 
       <article id="A0001"> 
        <description>Aardvark</description> 
       </article> 
       <quantity>1</quantity> 
      </orderline> 
      <orderline> 
       <article id="A0011"> 
        <description>Alpaca</description> 
       </article> 
       <quantity>10</quantity> 
      </orderline> 
     </orderlines> 
    </order> 
    <order id="2012_0002"> 

     <customer id="B0002"> 
      <name>Bristol Zoo Gardens</name> 
      <city>Bristol</city> 
      <country>UK</country> 
     </customer> 

     <date>2012-03-02</date> 

     <orderlines> 
      <orderline> 
       <article id="B0002"> 
        <description>Badger</description> 
       </article> 
       <quantity>2</quantity> 
      </orderline> 
      <orderline> 
       <article id="B0202"> 
        <description>Bee</description> 
       </article> 
       <quantity>200</quantity> 
      </orderline> 
     </orderlines> 
    </order> 
    </orders> 

XSLT :

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="no" indent="yes"/> 
    <xsl:template match="/"> 
     <xsl:element name="orders"> 
      <xsl:element name="order"> 
       <xsl:copy-of select="/*[local-name()='order']/*" 
       /> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

내 낙타 경로 :

3,516,는 ERROR_Output :

그래서 내가 ActiveMQ를까지 정확한 출력을 얻고있다. Activemq logid7이 xml에 오더 ID를 부여하지 않으면. 그 이후에 아래와 같은 오류가 발생합니다. 어디서 잘못되었는지 알려주세요.

thread #2 - file://zoodata/in] Splitter_Route     INFO split data : <order id="2012_0001"> 

      <customer id="A0001"> 
       <name>Antwerp Zoo</name> 
       <city>Antwerp</city> 
       <country>BE</country> 
      </customer> 

      <date>2012-03-01</date> 

      <orderlines> 
       <orderline> 
        <article id="A0001"> 
         <description>Aardvark</description> 
        </article> 
        <quantity>1</quantity> 
       </orderline> 
       <orderline> 
        <article id="A0011"> 
         <description>Alpaca</description> 
        </article> 
        <quantity>10</quantity> 
       </orderline> 
      </orderlines> 
     </order> 
    [ thread #2 - file://zoodata/in] Splitter_Route     INFO After XSLT : <?xml version="1.0" encoding="UTF-8"?><orders> 
    <order> 
    <customer id="A0001"> 
       <name>Antwerp Zoo</name> 
       <city>Antwerp</city> 
       <country>BE</country> 
      </customer> 
    <date>2012-03-01</date> 
    <orderlines> 
       <orderline> 
        <article id="A0001"> 
         <description>Aardvark</description> 
        </article> 
        <quantity>1</quantity> 
       </orderline> 
       <orderline> 
        <article id="A0011"> 
         <description>Alpaca</description> 
        </article> 
        <quantity>10</quantity> 
       </orderline> 
      </orderlines> 
    </order> 
    </orders> 

    [ thread #2 - file://zoodata/in] Splitter_Route     INFO <?xml version="1.0" encoding="UTF-8"?><orders> 
    <order> 
    <customer id="A0001"> 
       <name>Antwerp Zoo</name> 
       <city>Antwerp</city> 
       <country>BE</country> 
      </customer> 
    <date>2012-03-01</date> 
    <orderlines> 
       <orderline> 
        <article id="A0001"> 
         <description>Aardvark</description> 
        </article> 
        <quantity>1</quantity> 
       </orderline> 
       <orderline> 
        <article id="A0011"> 
         <description>Alpaca</description> 
        </article> 
        <quantity>10</quantity> 
       </orderline> 
      </orderlines> 
    </order> 
    </orders> 

[ thread #2 - file://zoodata/in] DefaultErrorHandler   ERROR Failed delivery for (MessageId: ID-LAPTOP-OO1BQC0T-63587-1514125730408-0-37 on ExchangeId: ID-LAPTOP-OO1BQC0T-63587-1514125730408-0-38). Exhausted after delivery attempt: 1 caught: org.apache.camel.builder.xml.InvalidXPathExpression: Invalid xpath: /order:order/@id. Reason: javax.xml.xpath.XPathExpressionException: com.sun.org.apache.xpath.internal.domapi.XPathStylesheetDOM3Exception: Prefix must resolve to a namespace: order 

    Message History 
    --------------------------------------------------------------------------------------------------------------------------------------- 
    RouteId    ProcessorId   Processor                  Elapsed (ms) 
    [_route1   ] [_route1   ] [file://zoodata/in                ] [  82] 
    [Splitter_Route ] [_log3    ] [log                   ] [   0] 
    [Splitter_Route ] [_to1    ] [xslt:file:F:\jboss_workspace\ZooPattern\data\order.xsl      ] [  15] 
    [Splitter_Route ] [_log2    ] [log                   ] [   0] 
    [Splitter_Route ] [_to2    ] [activemq:queue:zoodata              ] [  67] 
    [Splitter_Route ] [_log7    ] [log                   ] [   0] 
    [Splitter_Route ] [_setHeader1  ] [setHeader[orderId]               ] [   0] 

    Stacktrace 
    --------------------------------------------------------------------------------------------------------------------------------------- 
    org.apache.camel.builder.xml.InvalidXPathExpression: Invalid xpath: /order:order/@id. Reason: javax.xml.xpath.XPathExpressionException: com.sun.org.apache.xpath.internal.domapi.XPathStylesheetDOM3Exception: Prefix must resolve to a namespace: order 
     at org.apache.camel.builder.xml.XPathBuilder.evaluateAs(XPathBuilder.java:769) 
     at org.apache.camel.builder.xml.XPathBuilder.evaluate(XPathBuilder.java:750) 
     at org.apache.camel.builder.xml.XPathBuilder.evaluate(XPathBuilder.java:165) 
     at org.apache.camel.processor.SetHeaderProcessor.process(SetHeaderProcessor.java:52) 

답변

0

오류는 당신이 설정 당신이 당신의 <camelContext>으로 XML 파일에 수행 order 네임 스페이스를해야 함을 의미

Prefix must resolve to a namespace: order 

을 말한다. 거기에 속성으로 추가하십시오.

<camelContext .... xmlns:order="http://bla bla"> 

값은 일반적으로 일부 웹 URL 인 네임 스페이스의 고유 URI입니다.

그러나 XML 입력은 네임 스페이스를 사용하지 않으므로 문제는 setHeader의 xpath가 XML 입력을 사용하고 있다는 것입니다. 그러나 당신은 제거해야하므로

<xpath resultType="java.lang.String">/order:order/@id</xpath> 

는, 예를 들어 콜론 order:

<xpath resultType="java.lang.String">/order/@id</xpath> 

네임 스페이스 접두사입니다되어야한다고를 제거하고 간단한 수정을되어야한다 익명 XPath를 사용한다.