Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

dom java xml getAttributeByID

Thread view: 
rafiamar@gmail.com - 04 Jan 2006 08:37 GMT
hello,
if anyone could help me i'll be grateful
i have this xml:

<?xml version="1.0" encoding="UTF-8"?>
<appage_db>
  <cycle id="Pete">
                 <machine>"t" </machine>
                 <passwd>"403" </passwd>
                 <user>"766" </user>
  </cycle>
  <cycle id="Pro">
                 <machine>"p" </machine>
                 <passwd>"411" </passwd>
                 <user>"766" </user>
  </cycle>
</appage_db>

and i have this code:

class ProcessFruits {
  public static void main (String[] args) {
    try {
         javax.xml.parsers.DocumentBuilderFactory factory =
javax.xml.parsers.DocumentBuilderFactory.newInstance();
         javax.xml.parsers.DocumentBuilder domBuilder =
factory.newDocumentBuilder();
        org.w3c.dom.Document xmlDocument = domBuilder.parse(new
java.io.File("c:\\fff_config.xml"));
         org.w3c.dom.NodeList Cycle =
xmlDocument.getElementsByTagName("cycle");
          System.out.println("Found " + Cycle.getLength() + "
elements:");

         for (int i = 0; i < Cycle.getLength(); i++) {

           System.out.println("<Cycle> element " + (i + 1) + ":");
           org.w3c.dom.NodeList Descendants =
((org.w3c.dom.Element)Cycle.item(i)).getElementsByTagName("*");

           for (int j = 0; j < Descendants.getLength(); j++) {
             org.w3c.dom.Node node = Descendants.item(j);
             if (node.getChildNodes().getLength() == 1) {
               System.out.println("element " + node.getNodeName() +
":" + node.getFirstChild().getNodeValue());
             }
           }

         }
    }
    catch (Exception e) {
        System.out.println(e);
    }
  } }

I need to get the elements only from the node which has the "id" i want

this program shows all elements.

thanks guys

Rafael Amar

Reply
Stefan Schulz - 04 Jan 2006 09:25 GMT
Ermmm... it might be just me, but i think you are getting what you
asked for, after all you ask for all children of tagname "cycle" and do
not mention any id anywhere.
Rafael Amar - 04 Jan 2006 10:11 GMT
Hi Stefan,
this program is showing all the children but i don't want all the
children just the children of the "cycle" which i'm sreaching for

like getAttributeByID("Pete);

and now it's showing all the children of all the id's.

Thanks
Rafael Amar
Stefan Schulz - 04 Jan 2006 13:56 GMT
Did you use a validating parser? I am not sure since i usually do a
manual ID->Element mapping (too much uncertainty if you use a schema),
but IIRC as soon as you use a non-validating parser, all bets are off.
inexplicable - 04 Jan 2006 10:19 GMT
It seems that you're using JDOM, well, I think using JDOM alone
could achieve your requirement by filtering the children manually,
or you can have the alternative to using XPATH for help, and
jaxen could be an assistant project in this aspect.
inexplicable - 04 Jan 2006 14:52 GMT
I think the following code could make sense.

Object jdomNode = ...; // Document, Element etc.
XPath path = new JDOMXPath("//cycle[@id]");

List results = path.selectNodes(jdomNode);

You can keep using JDOM, just plus Jaxen packages as well,
could help you to locate the elements you want by XPath.
Chris Smith - 04 Jan 2006 19:06 GMT
> It seems that you're using JDOM, well, I think using JDOM alone
> could achieve your requirement by filtering the children manually,
> or you can have the alternative to using XPATH for help, and
> jaxen could be an assistant project in this aspect.

There's no indication that anyone is using JDOM.  In fact, the fact that
Rafael posted some code that doesn't use JDOM could be a good indication
that he's not, in fact, using JDOM.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Andrew Thompson - 04 Jan 2006 11:09 GMT
> hello,
> if anyone ..

Perhaps someone on one of the other groups to which you
multi-posted this problem, can help.

[ Please refrain from multi-posting. ]

Signature

Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew

Rafael Amar - 04 Jan 2006 11:49 GMT
I sorry i didn't know i couldn't

Rafael Amar
Andrew Thompson - 04 Jan 2006 12:15 GMT
[multi-post]

> I sorry i didn't know i couldn't ..

You have the technology to do so, but it it is not generally
productive, nor encouraged.  People who do it regularly tend
to become ignored very quickly (though a single time, through
lack of knowledge, does not matter much).

If a message is relevant to two groups, it is better to cross-post.
[ Then to get extra 'cred'[1] you might also set follow-ups to the
single most connected group, and put a sentence in the post
mentioning which group you have set the follow-ups to. ]

[1] Crediblility or respect for politeness and forethought.

Unfortunately the advice given out by your web based interface to
usenet is not entirely clear, on this page
<http://groups.google.com/support/bin/answer.py?answer=12348&query=cross&topic=0&
type=f
>
it states..
"Few things annoy Usenet readers as much as multiple copies of
a posting appearing in multiple newsgroups (called "spamming").
A posting that is cross-posted (...good things about cross-posting)"

The thing they refer to as "spamming" has come to mean
"unsolicited advertising" (on any single group that
discourages or bans advertising), as opposed to posting
a message to a number of groups separately, that is
known as "multi-posting".
<http://www.google.com.au/search?hl=en&q=definition+%22multi-posting%22&btnG=Sear
ch&meta
=>

Unfortunately, the general technique of spammers *is* to
multi-post, to as many groups as they can ley their grubby
little software on, so I can understand why the terms have
not yet gained a clear distinction.

[ Hope you solve the technical problem with getAttribute. ]

Signature

Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew

Rafael Amar - 04 Jan 2006 13:01 GMT
i found the answer.....

for (int i = 0; i < Cycle.getLength(); i++) {

                    org.w3c.dom.NamedNodeMap ggg =
((org.w3c.dom.Element)Cycle.item(i)).getAttributes();

                String gggg = ggg.getNamedItem("ID").toString();
                int index =gggg.indexOf("Pete");
                if ( index == -1){
                    not found
                }
                else{found}

Thanks

Rafael Amar


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.