Xerces C++ help in GetNodeValue(). Beginner question....

Discussion in 'XML' started by David, Dec 17, 2003.

  1. David

    David Guest

    Hello ,
    I'm trying to parse an XML document a get spicific tags such as email in the
    code below. I'm using xerces 2.4. However I don't manage to get the value
    for the email. Can anybody help.

    thanks in advance,

    david

    ---------------
    This the output of the piece of code below

    Index:0 Value:0
    Index:1 Value:0
    Index:2 Value:0
    Index:3 Value:0
    Index:4 Value:0
    Index:5 Value:0

    -----------------
    This the XML file:

    <?xml version="1.0" encoding="UTF-8"?>
    <?proc-inst-1 'foo' ?>
    <personnel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation='personal.xsd'>

    <person id="Big.Boss">
    <name xml:base="foo/bar"><family xml:base="bar/bar">Boss</family>
    <given xml:base="car/bar">Big</given><?proc-inst-2 'foobar' ?></name>
    <email></email>
    <link subordinates="one.worker two.worker three.worker four.worker
    five.worker"/>
    </person>

    <person id="one.worker" xml:base="/auto/bar">
    <name xml:base="/car/foo/"><family xml:base="bar/bar">Worker</family>
    <given>One</given></name>
    <email></email>
    <link manager="Big.Boss"/>
    </person>

    <person id="two.worker" xml:base="http://www.example.com/car/car">
    <name xml:base="/bar/foo/"><family xml:base="foo/bar">Worker</family>
    <given>Two</given></name>
    <email></email>
    <link manager="Big.Boss"/>
    </person>

    <person id="three.worker">
    <name><family>Worker</family> <given>Three</given></name>
    <email></email>
    <link manager="Big.Boss"/>
    </person>

    <person id="four.worker">
    <name><family>Worker</family> <given>Four</given></name>
    <email></email>
    <link manager="Big.Boss"/>
    </person>

    <person id="five.worker">
    <name><family>Worker</family> <given>Five</given></name>
    <email></email>
    <link manager="Big.Boss"/>
    </person>

    </personnel>


    --------------------------
    This is a piece of the code

    Main.cpp
    ........
    DOMDocument *doc = parser->getDocument();
    DOMElement *root = doc->getDocumentElement();
    DOMNode *node;
    XMLCh* tmpstr;
    int len;
    int i;

    XMLString::transcode("email", tmpstr ,50);

    DOMNodeList *list = doc->getElementsByTagName(tmpstr);
    len = list->getLength();

    for (i=0; i< len ;i++)
    {
    //Returns DOMnode object
    node = list->item(i);

    const XMLCh* n = node->getNodeValue();
    cout << "Index:" << i << " Value:" << n <<endl;

    }


    delete parser;
    delete errHandler;

    XMLPlatformUtils::Terminate();


    return 0;


    }
    David, Dec 17, 2003
    #1
    1. Advertising

  2. I think you must get the value of text node (remember "in DOM everything is
    a node", thus text is also node) which in your case is firstChild of your
    email element i.e.
    something like:
    node.firstChild.NodeValue
    (inside your loop)

    with respect,
    Toni Uusitalo


    "David" <> wrote in message
    news:brqfgh$vv0$...
    > Hello ,
    > I'm trying to parse an XML document a get spicific tags such as email in

    the
    > code below. I'm using xerces 2.4. However I don't manage to get the value
    > for the email. Can anybody help.
    >
    > thanks in advance,
    >
    > david
    >
    > ---------------
    > This the output of the piece of code below
    >
    > Index:0 Value:0
    > Index:1 Value:0
    > Index:2 Value:0
    > Index:3 Value:0
    > Index:4 Value:0
    > Index:5 Value:0
    >
    > -----------------
    > This the XML file:
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <?proc-inst-1 'foo' ?>
    > <personnel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    > xsi:noNamespaceSchemaLocation='personal.xsd'>
    >
    > <person id="Big.Boss">
    > <name xml:base="foo/bar"><family xml:base="bar/bar">Boss</family>
    > <given xml:base="car/bar">Big</given><?proc-inst-2 'foobar' ?></name>
    > <email></email>
    > <link subordinates="one.worker two.worker three.worker four.worker
    > five.worker"/>
    > </person>
    >
    > <person id="one.worker" xml:base="/auto/bar">
    > <name xml:base="/car/foo/"><family xml:base="bar/bar">Worker</family>
    > <given>One</given></name>
    > <email></email>
    > <link manager="Big.Boss"/>
    > </person>
    >
    > <person id="two.worker" xml:base="http://www.example.com/car/car">
    > <name xml:base="/bar/foo/"><family xml:base="foo/bar">Worker</family>
    > <given>Two</given></name>
    > <email></email>
    > <link manager="Big.Boss"/>
    > </person>
    >
    > <person id="three.worker">
    > <name><family>Worker</family> <given>Three</given></name>
    > <email></email>
    > <link manager="Big.Boss"/>
    > </person>
    >
    > <person id="four.worker">
    > <name><family>Worker</family> <given>Four</given></name>
    > <email></email>
    > <link manager="Big.Boss"/>
    > </person>
    >
    > <person id="five.worker">
    > <name><family>Worker</family> <given>Five</given></name>
    > <email></email>
    > <link manager="Big.Boss"/>
    > </person>
    >
    > </personnel>
    >
    >
    > --------------------------
    > This is a piece of the code
    >
    > Main.cpp
    > .......
    > DOMDocument *doc = parser->getDocument();
    > DOMElement *root = doc->getDocumentElement();
    > DOMNode *node;
    > XMLCh* tmpstr;
    > int len;
    > int i;
    >
    > XMLString::transcode("email", tmpstr ,50);
    >
    > DOMNodeList *list = doc->getElementsByTagName(tmpstr);
    > len = list->getLength();
    >
    > for (i=0; i< len ;i++)
    > {
    > //Returns DOMnode object
    > node = list->item(i);
    >
    > const XMLCh* n = node->getNodeValue();
    > cout << "Index:" << i << " Value:" << n <<endl;
    >
    > }
    >
    >
    > delete parser;
    > delete errHandler;
    >
    > XMLPlatformUtils::Terminate();
    >
    >
    > return 0;
    >
    >
    > }
    >
    Toni Uusitalo, Dec 17, 2003
    #2
    1. Advertising

  3. David

    David Guest

    I have modified the code as follows but still doesn't work. ANy idea ?

    cout << "LIST:" << len <<endl;
    for (i=0; i< len ;i++)
    {
    //Returns DOMnode object
    node = list->item(i);
    const XMLCh* test = node->getFirstChild()->getNodeValue();
    cout << "Index:" << i << " Value:" << test <<" Type:" <<endl;

    }

    I get:
    Index:0 Value:0x806f6f8
    Index:1 Value:0x8070140
    Index:2 Value:0x8070b28
    Index:3 Value:0x8071290
    Index:4 Value:0x80719f8
    Index:5 Value:0x8072158


    ANy idea ???


    Toni Uusitalo wrote:

    > I think you must get the value of text node (remember "in DOM everything
    > is a node", thus text is also node) which in your case is firstChild of
    > your email element i.e.
    > something like:
    > node.firstChild.NodeValue
    > (inside your loop)
    >
    > with respect,
    > Toni Uusitalo
    >
    >
    > "David" <> wrote in message
    > news:brqfgh$vv0$...
    >> Hello ,
    >> I'm trying to parse an XML document a get spicific tags such as email in

    > the
    >> code below. I'm using xerces 2.4. However I don't manage to get the value
    >> for the email. Can anybody help.
    >>
    >> thanks in advance,
    >>
    >> david
    >>
    >> ---------------
    >> This the output of the piece of code below
    >>
    >> Index:0 Value:0
    >> Index:1 Value:0
    >> Index:2 Value:0
    >> Index:3 Value:0
    >> Index:4 Value:0
    >> Index:5 Value:0
    >>
    >> -----------------
    >> This the XML file:
    >>
    >> <?xml version="1.0" encoding="UTF-8"?>
    >> <?proc-inst-1 'foo' ?>
    >> <personnel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    >> xsi:noNamespaceSchemaLocation='personal.xsd'>
    >>
    >> <person id="Big.Boss">
    >> <name xml:base="foo/bar"><family xml:base="bar/bar">Boss</family>
    >> <given xml:base="car/bar">Big</given><?proc-inst-2 'foobar' ?></name>
    >> <email></email>
    >> <link subordinates="one.worker two.worker three.worker four.worker
    >> five.worker"/>
    >> </person>
    >>
    >> <person id="one.worker" xml:base="/auto/bar">
    >> <name xml:base="/car/foo/"><family xml:base="bar/bar">Worker</family>
    >> <given>One</given></name>
    >> <email></email>
    >> <link manager="Big.Boss"/>
    >> </person>
    >>
    >> <person id="two.worker" xml:base="http://www.example.com/car/car">
    >> <name xml:base="/bar/foo/"><family xml:base="foo/bar">Worker</family>
    >> <given>Two</given></name>
    >> <email></email>
    >> <link manager="Big.Boss"/>
    >> </person>
    >>
    >> <person id="three.worker">
    >> <name><family>Worker</family> <given>Three</given></name>
    >> <email></email>
    >> <link manager="Big.Boss"/>
    >> </person>
    >>
    >> <person id="four.worker">
    >> <name><family>Worker</family> <given>Four</given></name>
    >> <email></email>
    >> <link manager="Big.Boss"/>
    >> </person>
    >>
    >> <person id="five.worker">
    >> <name><family>Worker</family> <given>Five</given></name>
    >> <email></email>
    >> <link manager="Big.Boss"/>
    >> </person>
    >>
    >> </personnel>
    >>
    >>
    >> --------------------------
    >> This is a piece of the code
    >>
    >> Main.cpp
    >> .......
    >> DOMDocument *doc = parser->getDocument();
    >> DOMElement *root = doc->getDocumentElement();
    >> DOMNode *node;
    >> XMLCh* tmpstr;
    >> int len;
    >> int i;
    >>
    >> XMLString::transcode("email", tmpstr ,50);
    >>
    >> DOMNodeList *list = doc->getElementsByTagName(tmpstr);
    >> len = list->getLength();
    >>
    >> for (i=0; i< len ;i++)
    >> {
    >> //Returns DOMnode object
    >> node = list->item(i);
    >>
    >> const XMLCh* n = node->getNodeValue();
    >> cout << "Index:" << i << " Value:" << n <<endl;
    >>
    >> }
    >>
    >>
    >> delete parser;
    >> delete errHandler;
    >>
    >> XMLPlatformUtils::Terminate();
    >>
    >>
    >> return 0;
    >>
    >>
    >> }
    >>
    David, Dec 17, 2003
    #3
  4. Hmm. at least it seems like test is now pointing to some string/somewhere
    (DOMstring?), we're getting closer. ;-)

    check last two functions in this:

    http://www.diku.dk/hjemmesider/studerende/mccash/speciale/src/sb2/Parser.cpp.html

    they will help you to get textNodes from mixed content, should be useful.
    You might have to transcode your DOMstring nodeValue to appropriate C++
    type,
    I don't know anything about Xerces-C must admit nor I don't use C++ much.
    Maybe there's some example demonstrating this in Xerces docs?

    with respect,
    Toni Uusitalo


    "David" <> wrote in message
    news:brqih6$1ti$...
    > I have modified the code as follows but still doesn't work. ANy idea ?
    >
    > cout << "LIST:" << len <<endl;
    > for (i=0; i< len ;i++)
    > {
    > //Returns DOMnode object
    > node = list->item(i);
    > const XMLCh* test = node->getFirstChild()->getNodeValue();
    > cout << "Index:" << i << " Value:" << test <<" Type:" <<endl;
    >
    > }
    >
    > I get:
    > Index:0 Value:0x806f6f8
    > Index:1 Value:0x8070140
    > Index:2 Value:0x8070b28
    > Index:3 Value:0x8071290
    > Index:4 Value:0x80719f8
    > Index:5 Value:0x8072158
    >
    >
    > ANy idea ???
    >
    >
    > Toni Uusitalo wrote:
    >
    > > I think you must get the value of text node (remember "in DOM everything
    > > is a node", thus text is also node) which in your case is firstChild of
    > > your email element i.e.
    > > something like:
    > > node.firstChild.NodeValue
    > > (inside your loop)
    > >
    > > with respect,
    > > Toni Uusitalo
    > >
    > >
    > > "David" <> wrote in message
    > > news:brqfgh$vv0$...
    > >> Hello ,
    > >> I'm trying to parse an XML document a get spicific tags such as email

    in
    > > the
    > >> code below. I'm using xerces 2.4. However I don't manage to get the

    value
    > >> for the email. Can anybody help.
    > >>
    > >> thanks in advance,
    > >>
    > >> david
    > >>
    > >> ---------------
    > >> This the output of the piece of code below
    > >>
    > >> Index:0 Value:0
    > >> Index:1 Value:0
    > >> Index:2 Value:0
    > >> Index:3 Value:0
    > >> Index:4 Value:0
    > >> Index:5 Value:0
    > >>
    > >> -----------------
    > >> This the XML file:
    > >>
    > >> <?xml version="1.0" encoding="UTF-8"?>
    > >> <?proc-inst-1 'foo' ?>
    > >> <personnel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    > >> xsi:noNamespaceSchemaLocation='personal.xsd'>
    > >>
    > >> <person id="Big.Boss">
    > >> <name xml:base="foo/bar"><family xml:base="bar/bar">Boss</family>
    > >> <given xml:base="car/bar">Big</given><?proc-inst-2 'foobar' ?></name>
    > >> <email></email>
    > >> <link subordinates="one.worker two.worker three.worker four.worker
    > >> five.worker"/>
    > >> </person>
    > >>
    > >> <person id="one.worker" xml:base="/auto/bar">
    > >> <name xml:base="/car/foo/"><family

    xml:base="bar/bar">Worker</family>
    > >> <given>One</given></name>
    > >> <email></email>
    > >> <link manager="Big.Boss"/>
    > >> </person>
    > >>
    > >> <person id="two.worker" xml:base="http://www.example.com/car/car">
    > >> <name xml:base="/bar/foo/"><family

    xml:base="foo/bar">Worker</family>
    > >> <given>Two</given></name>
    > >> <email></email>
    > >> <link manager="Big.Boss"/>
    > >> </person>
    > >>
    > >> <person id="three.worker">
    > >> <name><family>Worker</family> <given>Three</given></name>
    > >> <email></email>
    > >> <link manager="Big.Boss"/>
    > >> </person>
    > >>
    > >> <person id="four.worker">
    > >> <name><family>Worker</family> <given>Four</given></name>
    > >> <email></email>
    > >> <link manager="Big.Boss"/>
    > >> </person>
    > >>
    > >> <person id="five.worker">
    > >> <name><family>Worker</family> <given>Five</given></name>
    > >> <email></email>
    > >> <link manager="Big.Boss"/>
    > >> </person>
    > >>
    > >> </personnel>
    > >>
    > >>
    > >> --------------------------
    > >> This is a piece of the code
    > >>
    > >> Main.cpp
    > >> .......
    > >> DOMDocument *doc = parser->getDocument();
    > >> DOMElement *root = doc->getDocumentElement();
    > >> DOMNode *node;
    > >> XMLCh* tmpstr;
    > >> int len;
    > >> int i;
    > >>
    > >> XMLString::transcode("email", tmpstr ,50);
    > >>
    > >> DOMNodeList *list = doc->getElementsByTagName(tmpstr);
    > >> len = list->getLength();
    > >>
    > >> for (i=0; i< len ;i++)
    > >> {
    > >> //Returns DOMnode object
    > >> node = list->item(i);
    > >>
    > >> const XMLCh* n = node->getNodeValue();
    > >> cout << "Index:" << i << " Value:" << n <<endl;
    > >>
    > >> }
    > >>
    > >>
    > >> delete parser;
    > >> delete errHandler;
    > >>
    > >> XMLPlatformUtils::Terminate();
    > >>
    > >>
    > >> return 0;
    > >>
    > >>
    > >> }
    > >>

    >
    Toni Uusitalo, Dec 17, 2003
    #4
  5. I browsed Xerces-C docs abit
    (http://xml.apache.org/xerces-c/ApacheDOMC BindingL3.html) and noticed that
    there's
    function getTextContent(). This is DOM 3 feature I believe, it doesn't
    hurt to get used to the idea that "everything is a node" though - that's
    the way DOM works.

    "David" <> wrote in message
    news:brqih6$1ti$...
    > I have modified the code as follows but still doesn't work. ANy idea ?
    >
    > cout << "LIST:" << len <<endl;
    > for (i=0; i< len ;i++)
    > {
    > //Returns DOMnode object
    > node = list->item(i);
    > const XMLCh* test = node->getFirstChild()->getNodeValue();
    > cout << "Index:" << i << " Value:" << test <<" Type:" <<endl;
    >
    > }
    >
    > I get:
    > Index:0 Value:0x806f6f8
    > Index:1 Value:0x8070140
    > Index:2 Value:0x8070b28
    > Index:3 Value:0x8071290
    > Index:4 Value:0x80719f8
    > Index:5 Value:0x8072158
    >
    >
    > ANy idea ???
    >
    >
    > Toni Uusitalo wrote:
    >
    > > I think you must get the value of text node (remember "in DOM everything
    > > is a node", thus text is also node) which in your case is firstChild of
    > > your email element i.e.
    > > something like:
    > > node.firstChild.NodeValue
    > > (inside your loop)
    > >
    > > with respect,
    > > Toni Uusitalo
    > >
    > >
    > > "David" <> wrote in message
    > > news:brqfgh$vv0$...
    > >> Hello ,
    > >> I'm trying to parse an XML document a get spicific tags such as email

    in
    > > the
    > >> code below. I'm using xerces 2.4. However I don't manage to get the

    value
    > >> for the email. Can anybody help.
    > >>
    > >> thanks in advance,
    > >>
    > >> david
    > >>
    > >> ---------------
    > >> This the output of the piece of code below
    > >>
    > >> Index:0 Value:0
    > >> Index:1 Value:0
    > >> Index:2 Value:0
    > >> Index:3 Value:0
    > >> Index:4 Value:0
    > >> Index:5 Value:0
    > >>
    > >> -----------------
    > >> This the XML file:
    > >>
    > >> <?xml version="1.0" encoding="UTF-8"?>
    > >> <?proc-inst-1 'foo' ?>
    > >> <personnel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    > >> xsi:noNamespaceSchemaLocation='personal.xsd'>
    > >>
    > >> <person id="Big.Boss">
    > >> <name xml:base="foo/bar"><family xml:base="bar/bar">Boss</family>
    > >> <given xml:base="car/bar">Big</given><?proc-inst-2 'foobar' ?></name>
    > >> <email></email>
    > >> <link subordinates="one.worker two.worker three.worker four.worker
    > >> five.worker"/>
    > >> </person>
    > >>
    > >> <person id="one.worker" xml:base="/auto/bar">
    > >> <name xml:base="/car/foo/"><family

    xml:base="bar/bar">Worker</family>
    > >> <given>One</given></name>
    > >> <email></email>
    > >> <link manager="Big.Boss"/>
    > >> </person>
    > >>
    > >> <person id="two.worker" xml:base="http://www.example.com/car/car">
    > >> <name xml:base="/bar/foo/"><family

    xml:base="foo/bar">Worker</family>
    > >> <given>Two</given></name>
    > >> <email></email>
    > >> <link manager="Big.Boss"/>
    > >> </person>
    > >>
    > >> <person id="three.worker">
    > >> <name><family>Worker</family> <given>Three</given></name>
    > >> <email></email>
    > >> <link manager="Big.Boss"/>
    > >> </person>
    > >>
    > >> <person id="four.worker">
    > >> <name><family>Worker</family> <given>Four</given></name>
    > >> <email></email>
    > >> <link manager="Big.Boss"/>
    > >> </person>
    > >>
    > >> <person id="five.worker">
    > >> <name><family>Worker</family> <given>Five</given></name>
    > >> <email></email>
    > >> <link manager="Big.Boss"/>
    > >> </person>
    > >>
    > >> </personnel>
    > >>
    > >>
    > >> --------------------------
    > >> This is a piece of the code
    > >>
    > >> Main.cpp
    > >> .......
    > >> DOMDocument *doc = parser->getDocument();
    > >> DOMElement *root = doc->getDocumentElement();
    > >> DOMNode *node;
    > >> XMLCh* tmpstr;
    > >> int len;
    > >> int i;
    > >>
    > >> XMLString::transcode("email", tmpstr ,50);
    > >>
    > >> DOMNodeList *list = doc->getElementsByTagName(tmpstr);
    > >> len = list->getLength();
    > >>
    > >> for (i=0; i< len ;i++)
    > >> {
    > >> //Returns DOMnode object
    > >> node = list->item(i);
    > >>
    > >> const XMLCh* n = node->getNodeValue();
    > >> cout << "Index:" << i << " Value:" << n <<endl;
    > >>
    > >> }
    > >>
    > >>
    > >> delete parser;
    > >> delete errHandler;
    > >>
    > >> XMLPlatformUtils::Terminate();
    > >>
    > >>
    > >> return 0;
    > >>
    > >>
    > >> }
    > >>

    >
    Toni Uusitalo, Dec 17, 2003
    #5
    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. Manuel
    Replies:
    0
    Views:
    545
    Manuel
    Aug 28, 2003
  2. Peter Chatterton

    DOM Problem with getNodeValue()

    Peter Chatterton, Oct 19, 2003, in forum: Java
    Replies:
    9
    Views:
    2,796
    Peter Chatterton
    Oct 20, 2003
  3. Bernard Kufluk

    problem with getNodeValue

    Bernard Kufluk, Apr 21, 2004, in forum: XML
    Replies:
    1
    Views:
    504
    Bernard Kufluk
    Apr 21, 2004
  4. Waseem
    Replies:
    0
    Views:
    837
    Waseem
    Jul 6, 2004
  5. Waseem

    getNodeValue Problem -Xerces Perl

    Waseem, Jul 6, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    135
    Waseem
    Jul 6, 2004
Loading...

Share This Page