Sent: Tue 17/05/2005 16:44
To: 'Offiah Raymond, Slough'; dom4j-user@lists.sourceforge.net
Subject: RE: [dom4j-user] Parsing namespaces .... how d'you do it?
Raymond,
You have to include namespace references in the xpath
expressions. Also,
xpath does not handle using the default namespace
so you will have to define
a prefix for the
http://www.pensions.gov.uk/EconomicsAndFinance/Personal/Pensions/Regulation
namespace. You can either do this in code or in the source
document. Here
is an example if you do it in code:
document.getRootElement().add(DocumentHelper.createNamespace(
"prefix",
"http://www.pensions.gov.uk/EconomicsAndFinance/Personal/Pensions/Regulation
"));
Node registyNumber =
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:PensionSchem
eRegistryNumber");
Node name
=
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:Name");
Node addressLine1 =
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:Address/pref
ix:UKaddress/govADP:Line[1]");
Node addressLine2 =
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:Address/pref
ix:UKaddress/govADP:Line[2]");
Node addressLine3 =
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:Address/pref
ix:UKaddress/govADP:Line[3]");
Node addressLine4 =
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:Address/pref
ix:UKaddress/govADP:Line[4]");
Node addressLine5 =
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:Address/pref
ix:UKaddress/govADP:Line[5]");
Node postcode =
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:Address/pref
ix:UKaddress/govADP:PostCode");
Good luck,
Mark
-----Original Message-----
From: Offiah Raymond, Slough
[mailto:[EMAIL PROTECTED]]
Sent:
Tuesday, May 17, 2005 11:27 AM
To: dom4j-user@lists.sourceforge.net
Subject: [dom4j-user] Parsing namespaces .... how d'you do it?
Hello there.
I wonder if someone could help me out here.
I'm trying to parse an XML file, using DOM4J/XPath, and I'm
having trouble
with the namespaces defined in the file:
<PensionSchemeReturnRequest
xmlns="http://www.pensions.gov.uk/EconomicsAndFinance/Personal/Pensions/Regu
lation"
xmlns:govADP="http://www.pensions.gov.uk/people/AddressAndPersonalDetails"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.pensions.gov.uk/EconomicsAndFinance/Personal/
Pensions/Regulation
\OccupationalPensionSchemeReturnRequest-v1.3.xsd"
regulatoryReference="57437337">
<PensionSchemeDetails>
<PensionSchemeRegistryNumber>10000047</PensionSchemeRegistryNumber>
<Name>John Smith</Name>
<Address AddressUsage="work">
<UKaddress>
<govADP:Line>Code
House</govADP:Line>
<govADP:Line>Smith Streett</govADP:Line>
<govADP:Line>Deaconsfield</govADP:Line>
<govADP:PostCode>DE1
TR5P</govADP:PostCode>
</UKaddress>
</Address>
</PensionSchemeDetails>
</PensionSchemeReturnRequest>
... with this bit of code:
Node registyNumber =
document.selectSingleNode("//PensionSchemeDetails/PensionSchemeRegistryNumbe
r");
Node name =
document.selectSingleNode("//PensionSchemeDetails/Name");
Node addressLine1 =
document.selectSingleNode("//PensionSchemeDetails/Address/UKaddress/Line[1]"
);
Node addressLine2 =
document.selectSingleNode("//PensionSchemeDetails/Address/UKaddress/Line[2]"
);
Node addressLine3 =
document.selectSingleNode("//PensionSchemeDetails/Address/UKaddress/Line[3]"
);
Node addressLine4 =
document.selectSingleNode("//PensionSchemeDetails/Address/UKaddress/Line[4]"
);
Node addressLine5 =
document.selectSingleNode("//PensionSchemeDetails/Address/UKaddress/Line[5]"
);
Node postcode =
document.selectSingleNode("//PensionSchemeDetails/Address/UKaddress/PostCode
");
If I strip out all the namespace stuff, it works OK.
How do I parse XML files that are namespaced to the hilt ....
Thanks in advance
Ray
Scanned for viruses by MessageLabs. The integrity and security
of this
message cannot be guaranteed. This email is intended for the named
recipient
only, and may contain confidential information and proprietary
material. Any
unauthorised use or disclosure is prohibited.
-------------------------------------------------------
This
SF.Net email is sponsored by Oracle Space Sweepstakes
Want to be the first
software developer in space?
Enter now for the Oracle Space Sweepstakes!
http://ads.osdn.com/?ad_id=7412&alloc_id=16344&op=click
_______________________________________________
dom4j-user
mailing list
dom4j-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dom4j-user
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify Adgistics Ltd by replying to the original sender of this mail. This message may contain confidential information and is intended only for the individual named. If you are not the named addressee you should delete this e-mail and not disseminate, distribute or copy this e-mail.