doc.getElementById()

Discussion in 'XML' started by Paolo, Mar 29, 2007.

  1. Paolo

    Paolo Guest

    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
     
    Paolo, Mar 29, 2007
    #1
    1. Advertising

  2. Paolo schrieb:
    > I have a problem using the method getElementById:


    See the documentation for this method at
    <http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-getElBId>:

    > 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.

    --
    Johannes Koch
    In te domine speravi; non confundar in aeternum.
    (Te Deum, 4th cent.)
     
    Johannes Koch, Mar 29, 2007
    #2
    1. Advertising

  3. Paolo

    Paolo Guest

    On 29 Mar, 11:15, Johannes Koch <> wrote:
    > Paolo schrieb:
    >
    > > I have a problem using the method getElementById:

    >
    > See the documentation for this method at
    > <http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-...>:
    >
    > > 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.
     
    Paolo, Mar 29, 2007
    #3
  4. Paolo wrote:

    > 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.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Mar 29, 2007
    #4
  5. Paolo schrieb:
    > 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.

    --
    Johannes Koch
    In te domine speravi; non confundar in aeternum.
    (Te Deum, 4th cent.)
     
    Johannes Koch, Mar 29, 2007
    #5
  6. Paolo

    Paolo Guest

    On 29 Mar, 14:12, Martin Honnen <> wrote:
    > Paolo wrote:
    > > 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.


    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
     
    Paolo, Mar 29, 2007
    #6
  7. Paolo wrote:

    > <!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.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Mar 29, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Wallace
    Replies:
    0
    Views:
    496
    John Wallace
    Jul 22, 2003
  2. Matt
    Replies:
    3
    Views:
    548
    Tor Iver Wilhelmsen
    Sep 17, 2004
  3. Praveen Chhangani

    Converting a org.jdom DOC to org.w3c DOC

    Praveen Chhangani, Aug 5, 2003, in forum: XML
    Replies:
    2
    Views:
    1,017
    Johannes Koch
    Aug 7, 2003
  4. News123
    Replies:
    0
    Views:
    307
    News123
    Feb 6, 2010
  5. Replies:
    4
    Views:
    142
    Garrett Smith
    Jan 25, 2009
Loading...

Share This Page