View Full Version : dom4j question

July 28th, 2011, 06:55 PM
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=\"https://www.example.com/index.php/myaccounts/checkoutRequestFail\">
<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() );

July 28th, 2011, 10:51 PM
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