Problem parsing a xml using Jdom

Hi all,

I am reposting it as I was told to format the code and send it again.

I am trying to parse a xml file using a jdom java code.This code works fine if I remove xmlns attribute in the root element. (I get the expected result) .If the "xmlns" attribute is put in the xml as it should be then the parsing and retrieving returns null. Please tell me how to fix this issue.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Xml

<process name="CreateKBEntryService" targetNamespace="http://serena.com/CreateKBEntryService" suppressJoinFailure="yes" xmlns:tns="http://serena.com/CreateKBEntryService" xmlns="http://schemas.xmlsoap.org/ws/2003/03/business-process/" xmlns:bpelx="http://schemas.oracle.com/bpel/extension" xmlns:ora="http://schemas.oracle.com/xpath/extension" xmlns:nsxml0="http://localhost:8080/axis/services/CreateKBEntryService" xmlns:nsxml1="http://DefaultNamespace" xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/">

<partnerLinks>

<partnerLink name="client" partnerLinkType="tns:CreateKBEntryService" myRole="CreateKBEntryServiceProvider"/>

<partnerLink name="CreateKBEntryPartnerLink" partnerLinkType="nsxml0:CreateKBEntryLink" partnerRole="CreateKBEntryProvider"/>

</partnerLinks>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

Java:

import java.io.*;

import java.util.*;

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.input.SAXBuilder;

public class sample1 {

public static void main(String[] args) throws Exception {

// create a XML parser and read the XML file

SAXBuilder oBuilder = new SAXBuilder();

Document oDoc = oBuilder.build(new File("**xml file location**"));

Element root = oDoc.getRootElement();

System.out.println(root.getName());

String tgtns= root.getAttributeValue("targetNamespace");

System.out.println("tgt ns "+ tgtns);

List list= root.getChildren("partnerLinks");

Iterator it1= list.iterator();

System.out.println("Iterator 1 - "+list.size());

while(it1.hasNext()){

Element partnerlinks = (Element)it1.next();

List list2= partnerlinks.getChildren("partnerLink");

System.out.println("iterator 2 - "+list2.size());

}

}

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Result:

Without Xmlns in xml file(Expected and correct output)

process

tgt ns http://serena.com/CreateKBEntryService

Iterator 1 - 1//expected and correct result that comes when I remove xmlns attribute from xml

iterator 2 - 2

Result with xmlns:

process

tgt ns http://serena.com/CreateKBEntryService

Iterator 1 - 0 //instead of 0 should return 1

[2813 byte] By [sudarsona] at [2007-10-2 3:28:57]
# 1

One suggestion to try:

Change your code to the following

Element root = oDoc.getRootElement();

System.out.println(root.getName());

String tgtns= root.getAttributeValue("targetNamespace");

System.out.println("tgt ns "+ tgtns);

Namespace ns = Namespace.getNamespace(

"http://schemas.xmlsoap.org/ws/2003/03/business-process/" );

List list= root.getChildren("partnerLinks", ns );

Iterator it1= list.iterator();

System.out.println("Iterator 1 - "+list.size());

The JavaDoc says for the one-arg getChildren()

If this target element has no nested elements with the given name outside a namespace, an empty List is returned.

Your elements have a namespace (the default one) so it should be worth a try.

Dave Patterson

d.pattersona at 2007-7-15 22:38:40 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...