Hi Gregory
Once you have found the nodes that you want it is probably as easy to
use the dom4j API set to read the data

E.g. in XPATH
Node nodePersonName = parmDoc.selectSingleNode( searchPath ); 
String lName = nodePersonName.valueOf( "LastName" ); 
String lNameAttrib = nodePersonName.valueOf( "LastName/@Type" );

Or 
Node nodePersonName = parmDoc.selectSingleNode( searchPath ); 
Node lastName = nodePersonName.element("LastName");
String lName = lastName.getText(); 
String lNameAttrib = nodePersonName.attributeValue( "Type" );

For the xpath queries you can use the node.selectSingleNode to do the
relative queries 
So for the city 
        searchPath = searchPath + "/AdministrativeArea[ @Type='State'
]";
        Node nodeCity = parmDoc.selectSingleNode( searchPath ); 
Becomes 
        Node nodeCity = countryNode.selectSingleNode(
AdministrativeArea[ @Type='State' ]);

Also do you really need to use the // at the start of the search. This
will seach every node in the document!

Mike Skells
Ebizz consulting 
> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED]] On Behalf Of 
> Gregory Dulski (s)
> Sent: Thursday 29 August 2002 16:54
> To: '[EMAIL PROTECTED]'
> Subject: [dom4j-user] newbie question
> 
> 
> I am new to XPATH and I have to convert a string containing 
> an XML tree into a document then search the document for 
> specific elements and attribute values. Here's a small 
> example of my code...  I just do not know how to access the 
> values of the FirstName, LastName elements and the '@Type' 
> attributes of these..
> 
> Please help.  Thank you.
> Greg
> 
> -- XML --
> 
> <CAP>
>   <RelationshipRecord>
>     <Customer>
>       <CustomerID/>
>       <NameDetails>
>         <PersonName>
>           <FirstName Type='Contains'/>
>           <LastName Type='BeginsWith'>Adl</LastName>
>        </PersonName>
>       </NameDetails>
>       <AddressDetails AddrType='Directory'>
>         <Country>
>           <CountryName>United States</CountryName>
>           <AdministrativeArea Type='State'>
>             <AdministrativeAreaName Type='Code'/>
>             <Locality Type='BeginsWith'/>
>             <PostalCode Type='BeginsWtih'>606</PostalCode>
>           </AdminstrativeArea>
>         </Country>
>       </AddressDetails>
>     </Customer>
>   </RelationshipRecord>
> </CAP>
> 
> -- code --
> 
> // create the document from the string
> Document parmDoc = DocumentHelper.parseText( xSearchParms ); 
> searchPath = 
> "//Parms/CAP/RelationshipRecord/Customer/NameDetails/PersonName";
> 
> // last name
> 
> Node nodePersonName = parmDoc.selectSingleNode( searchPath ); 
> String lName = nodePersonName.valueOf( "LastName" ); String 
> lNameAttrib = nodePersonName.valueOf( "@Type" );
>       
> // first name 
> Node nodeFname = parmDoc.selectSingleNode( searchPath + 
> "/FirstName" ); String fName = nodeFname.valueOf( "FirstName" 
> ); String fNameAttrib = nodeFname.valueOf( "@Type" );
> 
> // country
> searchPath = "//Parms/CAP/RelationshipRecord/Customer" + 
> "/AddressDetails/Country"; Node nodeCountry = 
> parmDoc.selectSingleNode( searchPath ); String country = 
> nodeCountry.valueOf( "CountryName" );
> 
> // city
> searchPath = searchPath + "/AdministrativeArea[ @Type='State' 
> ]"; Node nodeCity = parmDoc.selectSingleNode( searchPath ); 
> String city = nodeCity.valueOf( "Locality" ); String 
> cityAttrib = nodeCity.valueOf( "@Type" );
> 
> 
> 
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf 
> _______________________________________________
> dom4j-user mailing list
> [EMAIL PROTECTED] 
> https://lists.sourceforge.net/lists/listinfo/d> om4j-user
> 




-------------------------------------------------------
This sf.net email is sponsored by: OSDN - Tired of that same old
cell phone?  Get a new here for FREE!
https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to