Results 1 to 2 of 2

Thread: dom4j question

  1. #1
    Join Date
    Jun 2011

    dom4j question

    I have an xml message which i need to parse. However attributeValue is the wrong thing to look for.

    this is probably relatively basic but can someone who knows a bit about dom4j tell me how i can get the values for each field/node/element?

            <?xml version=\"1.0\" encoding=\"UTF-8\"?>
            <profileCheckoutRequest \">
             <cancelUrl page=\"\">
            <description>Potash Shipment</description>
    this is the code i have so far...

    Document d = DocumentHelper.parseText(decodedMessage);
    Element rootel = d.getRootElement();
    Element el = (Element)rootel.selectSingleNode("merchantCustomerId");
    fout.write( ("xml is: " + el.attributeValue("merchantCustomerId") + "\n").getBytes() );

  2. #2
    Join Date
    Apr 2008

    Re: dom4j question

    Firstly, your XML is invalid. Your root node is bogus ...

    <profileCheckoutRequest \">
    Secondly, merchantCustomerId is an element not an attribute so you cannot use attributeValue to obtain its value.

    Have a look at the following example (untested but it should be fine) ...

     <thing id="1">
    Element root = d.getRootElement();
    Element thing = (Element)d.selectSingleNode("thing");
    String thingId = thing.attributeValue("id"); // 1
    Element item = (Element)thing.selectSingleNode("item");
    String itemText = item.getText(); // Something
    Go you good thing!

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts