Editing XML file with JDOM, null reference to Root

A

Alessandro

Hello,

I need to read an XML file using JDOM. Issue is that I get a "null"
reference of Root node.
I was asking if this was due to the not existence of schema definition
file (http://www.myserver/ReportRequest.xsd) or if it was for other
reasons.

Is there also any workaround to browse and process an XML file without
having XSD file ?


//START CODE

<?xml version="1.0"?>
<ReportRequest xmlns="http://www.myserver/ReportRequest.xsd">
<ReportTemplate>
<ReportAction>Replace</ReportAction>
<Name>TimeSeries</Name>
...
</ReportTemplate>
</ReportRequest>


***
....
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
Document doc=docBuilder.parse(filePath);

Node root=doc.getDocumentElement();
....

//END CODE

Thanks all,
best rgds
Ale
 
J

John B. Matthews

Alessandro said:
Hello,

I need to read an XML file using JDOM. Issue is that I get a "null"
reference of Root node.
I was asking if this was due to the not existence of schema definition
file (http://www.myserver/ReportRequest.xsd) or if it was for other
reasons.

Is there also any workaround to browse and process an XML file without
having XSD file ?

I see what you mean; getDoctype() returns null:

<http://java.sun.com/javase/6/docs/api/org/w3c/dom/Document.html>

You can still iterate through the NodeList returned by the document's
getElementsByTagName() method, if that helps.
 
A

Arne Vajhøj

Alessandro said:
I need to read an XML file using JDOM. Issue is that I get a "null"
reference of Root node.
I was asking if this was due to the not existence of schema definition
file (http://www.myserver/ReportRequest.xsd) or if it was for other
reasons.

Is there also any workaround to browse and process an XML file without
having XSD file ?
<?xml version="1.0"?>
<ReportRequest xmlns="http://www.myserver/ReportRequest.xsd">
<ReportTemplate>
<ReportAction>Replace</ReportAction>
<Name>TimeSeries</Name>
...
</ReportTemplate>
</ReportRequest>
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
Document doc=docBuilder.parse(filePath);
Node root=doc.getDocumentElement();

You are using W3C DOM not JDOM.

You are not actually using a schema - you are using a default
namespace (identified by the URL of your schema).

And root is not null - it just happen to have a null value
like all other element nodes.

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlParse {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
Document doc=docBuilder.parse("C:\\z.xml");
Node root = doc.getDocumentElement();
System.out.println(root);
NodeList childs = root.getChildNodes();
for(int i = 0; i < childs.getLength(); i++) {
if(childs.item(i).getNodeType() == Node.ELEMENT_NODE) {
Element elm = (Element)childs.item(i);
System.out.println(elm);
NodeList childs2 = elm.getChildNodes();
for(int j = 0; j < childs2.getLength(); j++) {
if(childs2.item(j).getNodeType() ==
Node.ELEMENT_NODE) {
Element elm2 = (Element)childs2.item(j);
System.out.println(elm2);
NodeList childs3 = elm2.getChildNodes();
for(int k = 0; k < childs3.getLength(); k++) {
if(childs3.item(k).getNodeType() ==
Node.TEXT_NODE) {
Node n = childs3.item(k);
System.out.println(n);
}
}
}
}
}
}
}
}

output:

[ReportTemplate: null]
[ReportAction: null]
[#text: Replace]
[Name: null]
[#text: TimeSeries]

You see that only the text nodes has a non null value.

Arne
 
E

EricF

Hello,

I need to read an XML file using JDOM. Issue is that I get a "null"
reference of Root node.
I was asking if this was due to the not existence of schema definition
file (http://www.myserver/ReportRequest.xsd) or if it was for other
reasons.

Is there also any workaround to browse and process an XML file without
having XSD file ?


//START CODE

<?xml version="1.0"?>
<ReportRequest xmlns="http://www.myserver/ReportRequest.xsd">
<ReportTemplate>
<ReportAction>Replace</ReportAction>
<Name>TimeSeries</Name>
...
</ReportTemplate>
</ReportRequest>


***
....
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
Document doc=docBuilder.parse(filePath);

Node root=doc.getDocumentElement();
....

//END CODE

Thanks all,
best rgds
Ale

Are you sure doc is not null?

Don't you want root = doc.getRootElement();

Eric
 
J

John B. Matthews

[...]
Are you sure doc is not null?

Don't you want root = doc.getRootElement();
[...]

I think the OP is using org.w3c.dom.Document, rather than
javax.swing.text.DefaultStyledDocument.ElementBuffer.

Given the former, getDoctype() returns null.
 
M

Mike Schilling

EricF said:
Are you sure doc is not null?

If parse() doesn't throw an exception, doc won't be null.
Don't you want root = doc.getRootElement();

getDocumentElement() is correct. Check the javadoc for
org.w3c.dom.Document.
 
E

EricF

If parse() doesn't throw an exception, doc won't be null.

getDocumentElement() is correct. Check the javadoc for
org.w3c.dom.Document.
If the OP was using JDOM, hetRootElement() would be correct. But as several
others have posted, this is not JDOM.

Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top