Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java xml namespaces

Status
Not open for further replies.

tinac99

Programmer
Jan 17, 2002
58
US
Hi,

Will the following scripts in the java code(unavailableAgencyName = unavailabilityScheduleElement.getChild("OrganizationAgencyName").getText() ;)
work even if I don't reference the namespace prefixes(ns2,ns3,ns4) while getting the value of the element in the xml file?

Thanks,

Tina

---------------------------------------------------
xml file:
---------

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:UnavailableSchedules xmlns:ns1=" xmlns:ns2=" xmlns:ns3=" xmlns:ns4=" <ns2:UnavailableSchedule ReasonCode="M">
<ns2:OrganizationAgencyName>Orange County PD</ns2:OrganizationAgencyName>
<ns3:OrganizationBranchName>Newport Beach</ns3:OrganizationBranchName>
<ns3:IdentificationID>bfleck</ns3:IdentificationID>
<ns3:personGivenName>Bela</ns3:personGivenName>
<ns3:personMiddleName>Anton</ns3:personMiddleName>
<ns3:personSurName>Fleck</ns3:personSurName>
<ns2:StartDate>2008-05-26-07:00</ns2:StartDate>
<ns2:EndDate>2008-06-01-07:00</ns2:EndDate>
</ns2:UnavailableSchedule>
<ns2:UnavailableSchedule ReasonCode="T">
<ns2:OrganizationAgencyName>Orange County PD</ns2:OrganizationAgencyName>
<ns3:OrganizationBranchName>Newport Beach</ns3:OrganizationBranchName>
<ns3:IdentificationID>opeterson</ns3:IdentificationID>
<ns3:personGivenName>Oscar</ns3:personGivenName>
<ns3:personMiddleName></ns3:personMiddleName>
<ns3:personSurName>Peterson</ns3:personSurName>
<ns2:StartDate>2008-06-02-07:00</ns2:StartDate>
<ns2:EndDate>2008-06-08-07:00</ns2:EndDate>
</ns2:UnavailableSchedule>
</ns2:UnavailableSchedules>


---------------------------------------------
java file:
--------
import java.net.URL;

import org.jdom.* ;
import org.jdom.input.*;
import org.jdom.output.* ;

import java.util.*;
import java.io.*;

Document doc = builder.build(...);

Element root = doc.getRootElement();
List unavailableScheduleChildren = root.getChildren();

Iterator unavailableScheduleElements = unavailableScheduleChildren.iterator();

while(unavailableScheduleElements.hasNext())
{
Element unavailabilityScheduleElement = (Element)unavailableScheduleElements.next();
unavailableReasonCode=unavailabilityScheduleElement.getAttributeValue("ReasonCode");
unavailableAgencyName = unavailabilityScheduleElement.getChild("OrganizationAgencyName").getText() ;
unavailableBranchName = unavailabilityScheduleElement.getChild("OrganizationBranchName").getText() ;

}
 
>Will the following scripts...[abridged]... work even if I don't reference the namespace prefixes(ns2,ns3,ns4) while getting the value of the element in the xml file?
Using getChild method only, I don't think so.

If you want to retrieve the child with the info only of its local name, you can import as well the org.jdom.xpath.Path and use xpath to reach the node using local-name() xpath function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top