• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to determine element attribute

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<outletCommus>
<commu updateTimestamp="2001-11-20 18:43:07 CET" id="ES0200247">
<commufuncdef commufuncdefCode="Phone"/>
<commuChannel>+34 213 35 57</commuChannel>
</commu>
<commu updateTimestamp="2001-11-20 18:55:15 CET" id="ES0203247">
<commufuncdef commufuncdefCode="Homepage"/>
<commuChannel>www.mysite.com>
</commu>
<commu updateTimestamp="2001-11-20 18:43:35 CET" id="ES0201247">
<commufuncdef commufuncdefCode="Fax"/>
<commuChannel>+34 234 45 56 34</commuChannel>
</commu>
</outletCommus>
In the example given, How do I determine the attribute "commufuncdefCode" value. I need to know if my commufuncdefCode is a Phone, Email, Homepage etc. I am using a Dom parser by can't sem to determine this attribute code type as I require.
 
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using the DOM parser in Java, the following approach will get you there -
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import java.io.File;
import org.w3c.dom.*;
public class order{
public static int depth;
public static void showAttrValue(Element element, String attrName)
{
System.out.println("Element::"+element.getNodeName()+"->"+"Attr::"+attrName+"->"+
"Value::"+element.getAttribute(attrName));
}
public static void main (String args[]) {
File docFile = new File("bb.xml");
Document doc = null;
NamedNodeMap nnMap = null;
int i;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setExpandEntityReferences(false);
DocumentBuilder docb = dbf.newDocumentBuilder();
doc = docb.parse(docFile);
Element root = doc.getDocumentElement();
depth=0;
// get all the commufuncdef elements
NodeList nList = doc.getElementsByTagName("commufuncdef");
for(i=0; i<nList.getLength(); i++)
{
showAttrValue((Element)nList.item(i), "commufuncdefCode");
}
//recurseNodes(root);
}
catch (DOMException de)
{
System.out.println("DOM Exception ::"+de.toString());
}
catch (Exception e)
{
System.out.println("EXCEPTION::"+e.toString());
System.out.println(e.getClass());
}
}
}
Now that you know how to access the attributes, we can react in whatever way we want by writing some code.
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgot to mention. One of the elements shown here are not well-formed.
<commu updateTimestamp="2001-11-20 18:55:15 CET" id="ES0203247">
<commufuncdef commufuncdefCode="Homepage"/>
<!-- BELOW ELEMENT IS NOT WELL-FORMED -->
<commuChannel>www.mysite.com>
</commu>
You need to correc that before doing any DOM processing or the parser will throw an exception.
 
Fergus Red
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. This works a treat.
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic