doc.getElementById()

P

Paolo

I have a problem using the method getElementById:

This is my code:

dtd file: prova.dtd

<!ELEMENT utente (nome, password)>

hml file: prova.xml

<?xml version="1.0"?>
<!DOCTYPE lista>

<lista>
<utente id = 'A1'>
<nome> A1 </nome>
<password> pp </password>
</utente>

<utente id = 'A2'>
<nome> A2 </nome>
<password> pp </password>
</utente>
</lista>

java file: prova.java

import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import java.io.*;

public class prova
{
public static void main (String [] args)
{
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

Document doc = db.parse("http://localhost:8080/XML/prova.xml");

System.out.println(doc.getElementById("A1"));

}
catch (SAXException e)
{
e.printStackTrace();
}

catch (IOException e)
{

e.printStackTrace();
}

catch (ParserConfigurationException e)
{
e.printStackTrace();
}
}
}

output:
null

why it gives back null and not the node to me with the A1 code?

thanks
 
J

Johannes Koch

Paolo said:
I have a problem using the method getElementById:

See the documentation for this method at
Note: The DOM implementation must have information that says which attributes are of type ID. Attributes with the name "ID" are not of type ID unless so defined. Implementations that do not know whether attributes are of type ID or not are expected to return null.
[...]
This is my code:

dtd file: prova.dtd

<!ELEMENT utente (nome, password)>

You did not define the id attribute to be of type ID.
 
P

Paolo

Paolo said:
I have a problem using the method getElementById:

See the documentation for this method at
Note: The DOM implementation must have information that says which attributes are of type ID. Attributes with the name "ID" are not of type ID unless so defined. Implementations that do not know whether attributes are of type ID or not are expected to return null.
[...]

This is my code:
dtd file: prova.dtd
<!ELEMENT utente (nome, password)>

You did not define the id attribute to be of type ID.

I have not very understand what you have said to me.

I have corrected the in this mode:

<!ELEMENT utente (nome, password)>
<!ATTLIST utente ID ID #REQUIRED>

but the output of the java code retrun always null.
 
M

Martin Honnen

Paolo said:
I have corrected the in this mode:

<!ELEMENT utente (nome, password)>
<!ATTLIST utente ID ID #REQUIRED>

but the output of the java code retrun always null.

XML is case sensitive, if you have
<utente id = 'A1'>
then you need to define an attribute with name 'id' and not with name
'ID'. The type needs to be 'ID' however.
 
J

Johannes Koch

Paolo said:
I have corrected the in this mode:

<!ELEMENT utente (nome, password)>
<!ATTLIST utente ID ID #REQUIRED>

In the sample XML code you posted the attribute was named 'id' instead
of 'ID'. You need to declare an attribute 'id' to be of type ID.
Additionally you have to reference the document type definition from
within your XML by the system identifier (URI). You may also have to set
the document builder factory to 'validating', but I'm not sure if this
is necessary.
 
P

Paolo

XML is case sensitive, if you have
<utente id = 'A1'>
then you need to define an attribute with name 'id' and not with name
'ID'. The type needs to be 'ID' however.

It was various because I was making some tests.

<?xml version="1.0"?>
<!DOCTYPE lista>

<lista>
<utente ID = "A1">
<nome> A1 </nome>
<password> pp </password>
</utente>

<utente ID = "A2">
<nome> A2 </nome>
<password> pp </password>
</utente>
</lista>

<!ELEMENT utente (nome, password)>
<!ATTLIST utente ID ID #REQUIRED>

but the output is: null
 
M

Martin Honnen

Paolo said:
<!DOCTYPE lista>

<!ELEMENT utente (nome, password)>
<!ATTLIST utente ID ID #REQUIRED>

You need to link to the DTD e.g.
<!DOCTYPE lista SYSTEM "lista.dtd">
where lista.dtd then contains the element and attribute definitions.
 

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top