문제

been scratching my head for a while here...

So I have a Java application. In this application I need to read an XML file, get the Character Data from an element, pass it into a new DOM Document, change some of the elements, and convert the new Document back into CDATA, reattach it to the original message and send it off.

So... Here is the message I need to read in, and the function which reads it in:

private static String getCharacterDataFromElement(Node e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
      CharacterData cd = (CharacterData) child;
      System.out.println(cd.getBaseURI());
      System.out.println(cd.getData());
      return cd.getBaseURI();
    }
    return "error...";
}

And here is the xml file which needs to be changed

<RLSOLVE_MSG version="5.0">
<MESSAGE>
    <SOURCE_ID>DP01</SOURCE_ID>
    <TRANS_NUM>000001</TRANS_NUM>
</MESSAGE>
<POI_MSG type="interaction">
    <INTERACTION name="posPrintReceipt">
        <RECEIPT type="merchant" format="xml">
            <![CDATA[<RECEIPT>
  <AUTH_CODE>06130</AUTH_CODE>
  <CARD_SCHEME>VISA</CARD_SCHEME>
  <CURRENCY_CODE>GBP</CURRENCY_CODE>
  <CUSTOMER_PRESENCE>internet</CUSTOMER_PRESENCE>
  <FINAL_AMOUNT>1.00</FINAL_AMOUNT>
  <MERCHANT_NUMBER>8888888</MERCHANT_NUMBER>
  <PAN_NUMBER>454420******0382</PAN_NUMBER>
  <PAN_EXPIRY>12/15</PAN_EXPIRY>
  <TERMINAL_ID>04176421</TERMINAL_ID>
  <TOKEN>454420bbbbbkqrm0382</TOKEN>
  <TOTAL_AMOUNT>1.00</TOTAL_AMOUNT>
  <TRANSACTION_DATA_SOURCE>keyed</TRANSACTION_DATA_SOURCE>
  <TRANSACTION_DATE>14/02/2014</TRANSACTION_DATE>
  <TRANSACTION_NUMBER>000001</TRANSACTION_NUMBER>
  <TRANSACTION_RESPONSE>06130</TRANSACTION_RESPONSE>
  <TRANSACTION_TIME>17:13:17</TRANSACTION_TIME>
  <TRANSACTION_TYPE>purchase</TRANSACTION_TYPE>
  <VERIFICATION_METHOD>unknown</VERIFICATION_METHOD>
  <DUPLICATE>false</DUPLICATE>
</RECEIPT>]]>
            </RECEIPT>
        </INTERACTION>
    </POI_MSG>
</RLSOLVE_MSG>

When cd.getData() is executed, it returns "\n \t \t \t \t"

Aaaany ideas?

도움이 되었습니까?

해결책

Look closely at your XML. If I write them in one line it's actually

<RECEIPT type="merchant" format="xml">\n\t\t\t<![CDATA[...]]>\n\t\t\t</RECEIPT>

So the tree will actually look something like:

            RECEIPT
       /      |      \
 \n\t\t\t   CDATA   \n\t\t\t

So you got three children. As you are only getting the first child, you're only getting \n\t\t\t.

Loop through all the children and concat their data and you should have everything.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top