Java Code Examples for org.w3c.dom.Node

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project Agot-Java, under directory /src/main/java/got/pojo/.

Source file: GameInfo.java

  18 
vote

public void readConnectionsXML() throws ParserConfigurationException, SAXException, IOException {
  DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
  DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
  Document doc=dBuilder.parse(getClass().getResourceAsStream("/got/resource/xml/connection.xml"));
  doc.getDocumentElement().normalize();
  NodeList nList=doc.getElementsByTagName("Connection");
  for (int temp=0; temp < nList.getLength(); temp++) {
    Node nNode=nList.item(temp);
    String t1=nNode.getAttributes().getNamedItem("t1").getNodeValue();
    String t2=nNode.getAttributes().getNamedItem("t2").getNodeValue();
    getConnTerritories().get(t1).add(t2);
    getConnTerritories().get(t2).add(t1);
  }
}
 

Example 2

From project amplafi-sworddance, under directory /src/main/java/com/sworddance/util/.

Source file: AbstractXmlParser.java

  18 
vote

protected String getChildElementsContentByTagName(Node parent,boolean onlyAllowed,String... tagnames){
  NodeListImpl children=this.getChildElementsByTagName(parent,onlyAllowed,tagnames);
  StringBuilder builder=new StringBuilder();
  for (int index=0; index < children.getLength(); index++) {
    Node childNode=children.item(index);
    builder.append(childNode.getTextContent());
  }
  return builder.toString();
}
 

Example 3

From project anadix, under directory /anadix-core/src/main/java/org/anadix/impl/.

Source file: XHTMLReportFormatter.java

  18 
vote

private static Node createXHTMLHeading(Report report,Document doc){
  Node html=doc.appendChild(doc.createElement("html"));
  Node head=html.appendChild(doc.createElement("head"));
  Node title=head.appendChild(doc.createElement("title"));
  title.setTextContent(String.format("Anadix report for '%s'",report.getSource().getDescription()));
  Node style=head.appendChild(doc.createElement("style"));
  style.setTextContent(STYLE);
  return html.appendChild(doc.createElement("body"));
}
 

Example 4

From project android-client_1, under directory /src/com/googlecode/asmack/.

Source file: XMLUtils.java

  18 
vote

/** 
 * Return the first child of a node, based on name/namespace.
 * @param node The node to scan.
 * @param namespace The requested namespace, or null for no preference.
 * @param name The element name, or null for no preference.
 * @return The first matching node or null.
 */
public static Node getFirstChild(Node node,String namespace,String name){
  NodeList childNodes=node.getChildNodes();
  for (int i=0, l=childNodes.getLength(); i < l; i++) {
    Node child=childNodes.item(i);
    if (isInstance(child,namespace,name)) {
      return child;
    }
  }
  return null;
}
 

Example 5

From project Android_1, under directory /org.eclipse.ecf.android/src/org/eclipse/ecf/android/.

Source file: ServerConfigParser.java

  18 
vote

protected void processConnector(Node n,Connector c){
  List groupList=new ArrayList();
  findElementsNamed(n,GROUP_ELEMENT,groupList);
  for (Iterator i=groupList.iterator(); i.hasNext(); ) {
    Node node=(Node)i.next();
    String name=getAttributeValue(node,NAME_ATTR);
    if (name != null && !name.equals("")) {
      NamedGroup g=new NamedGroup(name);
      c.addGroup(g);
      g.setParent(c);
    }
  }
}
 

Example 6

From project any23, under directory /core/src/main/java/org/apache/any23/extractor/html/.

Source file: DomUtils.java

  18 
vote

/** 
 * Does a reverse walking of the DOM tree to generate a unique XPath expression leading to this node. The XPath generated is the canonical one based on sibling index: /html[1]/body[1]/div[2]/span[3] etc..
 * @param node the input node.
 * @return the XPath location of node as String.
 */
public static String getXPathForNode(Node node){
  final StringBuilder sb=new StringBuilder();
  Node parent=node;
  while (parent != null && parent.getNodeType() != Node.DOCUMENT_NODE) {
    sb.insert(0,"]");
    sb.insert(0,getIndexInParent(parent) + 1);
    sb.insert(0,"[");
    sb.insert(0,parent.getNodeName());
    sb.insert(0,"/");
    parent=parent.getParentNode();
  }
  return sb.toString();
}
 

Example 7

From project apb, under directory /modules/apb-base/src/apb/idegen/.

Source file: IdeaTask.java

  18 
vote

private static Element getElementByName(Element element,String name){
  Element result=null;
  NodeList children=element.getElementsByTagName(name);
  for (int i=0; i < children.getLength(); i++) {
    Node node=children.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) {
      result=(Element)node;
      break;
    }
  }
  return result;
}
 

Example 8

From project Archimedes, under directory /br.org.archimedes.io.xml/src/br/org/archimedes/io/xml/parsers/.

Source file: DimensionParser.java

  18 
vote

@Override public Element parse(Node node) throws ElementCreationException {
  NodeList nodesCollection=((org.w3c.dom.Element)node).getElementsByTagName("size");
  if (nodesCollection.getLength() == 1) {
    Node childNode=nodesCollection.item(0);
    this.size=XMLUtils.nodeToDouble(childNode);
    return super.parse(node);
  }
  return null;
}
 

Example 9

From project arquillian-container-jbossas, under directory /jbossas-managed-4.2/src/main/java/org/jboss/arquillian/container/jbossas/managed_4_2/.

Source file: ManagementViewParser.java

  18 
vote

private static List<String> extractServletNames(String descriptor) throws Exception {
  Document doc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(descriptor.getBytes()));
  XPathExpression xPathExpression=XPathFactory.newInstance().newXPath().compile("/web-app/servlet/servlet-name");
  NodeList nodes=(NodeList)xPathExpression.evaluate(doc,XPathConstants.NODESET);
  List<String> servletNames=new ArrayList<String>();
  for (int i=0; i < nodes.getLength(); i++) {
    Node node=nodes.item(i);
    servletNames.add(node.getTextContent());
  }
  return servletNames;
}
 

Example 10

From project arquillian-core, under directory /config/impl-base/src/test/java/org/jboss/arquillian/config/descriptor/impl/.

Source file: AssertXPath.java

  18 
vote

/** 
 * Assert that the specified XPath Expression resolves to the specified values. <br/><br/> Assertions:<br/> "ExpectedValue count should match found Node count" <br/>  "XPath content should match expected value" <br/>
 * @param xml The XML to assert against
 * @param expression XPath expression to extract
 * @param expectedValue The Expected values found by expression
 * @throws Exception XML/XPath related parse exceptions
 */
public static void assertXPath(String xml,String expression,Object... expectedValue) throws Exception {
  Document doc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
  XPathExpression xPathExpression=XPathFactory.newInstance().newXPath().compile(expression);
  NodeList nodes=(NodeList)xPathExpression.evaluate(doc,XPathConstants.NODESET);
  Assert.assertEquals("ExpectedValue count should match found Node count",expectedValue.length,nodes.getLength());
  for (int i=0; i < nodes.getLength(); i++) {
    Node node=nodes.item(i);
    Assert.assertEquals("XPath content should match expected value",String.valueOf(expectedValue[i]),node.getTextContent());
  }
}
 

Example 11

From project arquillian_deprecated, under directory /containers/jbossas-remote-5/src/main/java/org/jboss/arquillian/container/jbossas/remote_5_0/.

Source file: ManagementViewParser.java

  18 
vote

private static List<String> extractServletNames(String descriptor) throws Exception {
  Document doc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(descriptor.getBytes()));
  XPathExpression xPathExpression=XPathFactory.newInstance().newXPath().compile("/web-app/servlet/servlet-name");
  NodeList nodes=(NodeList)xPathExpression.evaluate(doc,XPathConstants.NODESET);
  List<String> servletNames=new ArrayList<String>();
  for (int i=0; i < nodes.getLength(); i++) {
    Node node=nodes.item(i);
    servletNames.add(node.getTextContent());
  }
  return servletNames;
}
 

Example 12

From project AsmackService, under directory /src/com/googlecode/asmack/.

Source file: XMLUtils.java

  18 
vote

/** 
 * Return the first child of a node, based on name/namespace.
 * @param node The node to scan.
 * @param namespace The requested namespace, or null for no preference.
 * @param name The element name, or null for no preference.
 * @return The first matching node or null.
 */
public static Node getFirstChild(Node node,String namespace,String name){
  NodeList childNodes=node.getChildNodes();
  for (int i=0, l=childNodes.getLength(); i < l; i++) {
    Node child=childNodes.item(i);
    if (isInstance(child,namespace,name)) {
      return child;
    }
  }
  return null;
}
 

Example 13

From project azure-sdk-for-java-samples, under directory /WAAD.WebSSO.JAVA/java/code/libraries/waad-federation/src/main/java/com/microsoft/samples/waad/federation/.

Source file: TrustedIssuersRepository.java

  18 
vote

public Iterable<TrustedIssuer> getTrustedIdentityProviderUrls() throws ParserConfigurationException, SAXException, IOException {
  List<TrustedIssuer> trustedIssuers=new ArrayList<TrustedIssuer>();
  DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
  DocumentBuilder documentBuilder=factory.newDocumentBuilder();
  Document document=documentBuilder.parse(getRepositoryStream());
  NodeList xmlTrustedIssuers=document.getFirstChild().getChildNodes();
  for (int i=0; i < xmlTrustedIssuers.getLength(); i++) {
    Node xmlTrustedIssuer=xmlTrustedIssuers.item(i);
    if (xmlTrustedIssuer instanceof Element) {
      trustedIssuers.add(new TrustedIssuer(xmlTrustedIssuer.getAttributes().getNamedItem(Constants.REPOSITORY_NAME_ATTRIBUTE).getTextContent(),xmlTrustedIssuer.getAttributes().getNamedItem(Constants.REPOSITORY_DISPLAY_NAME_ATTRIBUTE).getTextContent(),xmlTrustedIssuer.getAttributes().getNamedItem(Constants.REPOSITORY_REALM_ATTRIBUTE).getTextContent()));
    }
  }
  return trustedIssuers;
}
 

Example 14

From project Amoeba-for-Aladdin, under directory /src/java/com/meidusa/amoeba/config/.

Source file: DocumentUtil.java

  17 
vote

/** 
 * ??amoeba.xml?????property?name??????
 * @param current
 * @return
 */
public static BeanObjectEntityConfig loadBeanConfig(Element current){
  if (current == null) {
    return null;
  }
  BeanObjectEntityConfig beanConfig=new BeanObjectEntityConfig();
  NodeList children=current.getChildNodes();
  int childSize=children.getLength();
  beanConfig.setName(current.getAttribute("name"));
  Element element=DocumentUtil.getTheOnlyElement(current,"className");
  if (element != null) {
    beanConfig.setClassName(element.getTextContent());
  }
 else {
    beanConfig.setClassName(current.getAttribute("class"));
  }
  Map<String,Object> map=new HashMap<String,Object>();
  for (int i=0; i < childSize; i++) {
    Node childNode=children.item(i);
    if (childNode instanceof Element) {
      Element child=(Element)childNode;
      final String nodeName=child.getNodeName();
      if (nodeName.equals("property")) {
        String key=child.getAttribute("name");
        NodeList propertyNodes=child.getElementsByTagName("bean");
        if (propertyNodes.getLength() == 0) {
          String value=child.getTextContent();
          map.put(key,StringUtil.isEmpty(value) ? null : value.trim());
        }
 else {
          BeanObjectEntityConfig beanconfig=loadBeanConfig((Element)propertyNodes.item(0));
          map.put(key,beanconfig);
        }
      }
    }
  }
  beanConfig.setParams(map);
  return beanConfig;
}
 

Example 15

From project android-joedayz, under directory /Proyectos/LectorFeeds/src/com/android/joedayz/demo/.

Source file: XMLParser.java

  17 
vote

public LinkedList<HashMap<String,String>> parse(){
  DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
  LinkedList<HashMap<String,String>> entries=new LinkedList<HashMap<String,String>>();
  HashMap<String,String> entry;
  try {
    DocumentBuilder builder=factory.newDocumentBuilder();
    Document dom=builder.parse(this.url.openConnection().getInputStream());
    Element root=dom.getDocumentElement();
    NodeList items=root.getElementsByTagName("item");
    for (int i=0; i < items.getLength(); i++) {
      entry=new HashMap<String,String>();
      Node item=items.item(i);
      NodeList properties=item.getChildNodes();
      for (int j=0; j < properties.getLength(); j++) {
        Node property=properties.item(j);
        String name=property.getNodeName();
        if (name.equalsIgnoreCase("title")) {
          entry.put(Main.DATA_TITLE,property.getFirstChild().getNodeValue());
        }
 else         if (name.equalsIgnoreCase("link")) {
          entry.put(Main.DATA_LINK,property.getFirstChild().getNodeValue());
        }
      }
      entries.add(entry);
    }
  }
 catch (  Exception e) {
    throw new RuntimeException(e);
  }
  return entries;
}
 

Example 16

From project androidannotations, under directory /AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/helper/.

Source file: AndroidManifestFinder.java

  17 
vote

private List<String> extractComponentNames(String applicationPackage,NodeList componentNodes){
  List<String> componentQualifiedNames=new ArrayList<String>();
  for (int i=0; i < componentNodes.getLength(); i++) {
    Node activityNode=componentNodes.item(i);
    Node nameAttribute=activityNode.getAttributes().getNamedItem("android:name");
    String qualifiedName=manifestNameToValidQualifiedName(applicationPackage,nameAttribute);
    if (qualifiedName != null) {
      componentQualifiedNames.add(qualifiedName);
    }
 else {
      Messager messager=processingEnv.getMessager();
      if (nameAttribute != null) {
        messager.printMessage(Kind.NOTE,String.format("A class activity declared in the AndroidManifest.xml cannot be found in the compile path: [%s]",nameAttribute.getNodeValue()));
      }
 else {
        messager.printMessage(Kind.NOTE,String.format("The %d activity node in the AndroidManifest.xml has no android:name attribute",i));
      }
    }
  }
  return componentQualifiedNames;
}
 

Example 17

From project androidquery, under directory /src/com/androidquery/util/.

Source file: XmlDom.java

  17 
vote

private void serialize(Element e,XmlSerializer s,int depth,String spaces) throws Exception {
  String name=e.getTagName();
  writeSpace(s,depth,spaces);
  s.startTag("",name);
  if (e.hasAttributes()) {
    NamedNodeMap nm=e.getAttributes();
    for (int i=0; i < nm.getLength(); i++) {
      Attr attr=(Attr)nm.item(i);
      s.attribute("",attr.getName(),attr.getValue());
    }
  }
  if (e.hasChildNodes()) {
    NodeList nl=e.getChildNodes();
    int elements=0;
    for (int i=0; i < nl.getLength(); i++) {
      Node n=nl.item(i);
      short type=n.getNodeType();
switch (type) {
case Node.ELEMENT_NODE:
        serialize((Element)n,s,depth + 1,spaces);
      elements++;
    break;
case Node.TEXT_NODE:
  s.text(text(n));
break;
case Node.CDATA_SECTION_NODE:
s.cdsect(text(n));
break;
}
}
if (elements > 0) {
writeSpace(s,depth,spaces);
}
}
s.endTag("",name);
}
 

Example 18

From project apps-for-android, under directory /CLiCkin2DaBeaT/src/com/google/clickin2dabeat/.

Source file: C2B.java

  17 
vote

private void runC2B(Document c2b){
  Node root=c2b.getElementsByTagName("c2b").item(0);
  title=root.getAttributes().getNamedItem("title").getNodeValue();
  author=root.getAttributes().getNamedItem("author").getNodeValue();
  level=root.getAttributes().getNamedItem("level").getNodeValue();
  media=root.getAttributes().getNamedItem("media").getNodeValue();
  NodeList beats=c2b.getElementsByTagName("beat");
  targets=new ArrayList<Target>();
  for (int i=0; i < beats.getLength(); i++) {
    NamedNodeMap attribs=beats.item(i).getAttributes();
    double time=Double.parseDouble(attribs.getNamedItem("time").getNodeValue());
    int x=Integer.parseInt(attribs.getNamedItem("x").getNodeValue());
    int y=Integer.parseInt(attribs.getNamedItem("y").getNodeValue());
    String colorStr=attribs.getNamedItem("color").getNodeValue();
    targets.add(new Target(time,x,y,colorStr));
  }
  if ((beats.getLength() == 0) || forceEditMode) {
    displayCreateLevelAlert();
  }
 else {
    videoUri=Uri.parse(media);
    background.setVideoURI(videoUri);
  }
}
 

Example 19

From project arquillian-extension-persistence, under directory /impl/src/main/java/org/jboss/arquillian/persistence/core/configuration/.

Source file: PersistenceDescriptorParser.java

  17 
vote

public String obtainDataSourceName(final String descriptor){
  try {
    final Document document=factory.newDocumentBuilder().parse(new ByteArrayInputStream(descriptor.getBytes()));
    final NodeList persistenceUnits=document.getElementsByTagName("persistence-unit");
    if (persistenceUnits.getLength() > 1) {
      throw new MultiplePersistenceUnitsException("Multiple persistence units defined. Please specify default data source either in 'arquillian.xml' or by using @DataSource annotation");
    }
    final Node persistenceUnit=persistenceUnits.item(0);
    Node dataSource=getJtaDataSource(persistenceUnit);
    if (dataSource == null) {
      dataSource=getNonJtaDataSource(persistenceUnit);
    }
    return dataSource.getTextContent();
  }
 catch (  SAXException e) {
    throw new PersistenceDescriptorParsingException("Unable to parse descriptor " + descriptor,e);
  }
catch (  IOException e) {
    throw new PersistenceDescriptorParsingException("Unable to parse descriptor " + descriptor,e);
  }
catch (  ParserConfigurationException e) {
    throw new PersistenceDescriptorParsingException("Unable to parse descriptor " + descriptor,e);
  }
}
 

Example 20

From project aws-toolkit-for-eclipse, under directory /com.amazonaws.eclipse.core/src/com/amazonaws/eclipse/core/regions/.

Source file: RegionMetadataParser.java

  17 
vote

/** 
 * Parses the specified input stream and returns a list of the regions declared in it.
 * @param input The stream containing the region metadata to parse.
 * @return The list of parsed regions.
 */
public List<Region> parseRegionMetadata(InputStream input){
  Document document;
  try {
    DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder=factory.newDocumentBuilder();
    document=documentBuilder.parse(input);
  }
 catch (  Exception e) {
    throw new RuntimeException("Unable to parse region metadata file: " + e.getMessage(),e);
  }
  NodeList regionNodes=document.getElementsByTagName(REGION_TAG);
  List<Region> regions=new ArrayList<Region>();
  for (int i=0; i < regionNodes.getLength(); i++) {
    Node node=regionNodes.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
      Element element=(Element)node;
      regions.add(parseRegionElement(element));
    }
  }
  return regions;
}
 

Example 21

From project bbb-java, under directory /src/main/java/org/mconf/bbb/api/.

Source file: Meetings.java

  17 
vote

public int parse(String str) throws ParserConfigurationException, UnsupportedEncodingException, SAXException, IOException, DOMException, ParseException {
  meetings.clear();
  log.debug("parsing getMeetings response: {}",str);
  DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
  DocumentBuilder db=dbf.newDocumentBuilder();
  Document doc=db.parse(new ByteArrayInputStream(str.getBytes("UTF-8")));
  doc.getDocumentElement().normalize();
  Node first_node=doc.getFirstChild();
  if (first_node == null) {
    log.error("Parsing a non-XML response for getMeetings");
    return JoinServiceBase.E_MOBILE_NOT_SUPPORTED;
  }
  boolean check_return_code;
  if (first_node.getNodeName().equals("meetings")) {
    log.info("The given response is a mobile getMeetings");
    check_return_code=true;
  }
 else   if (first_node.getNodeName().equals("response")) {
    log.info("The given response is a default getMeetings, or it's an error response");
    NodeList return_code_list=doc.getElementsByTagName("returncode");
    if (return_code_list == null || return_code_list.getLength() <= 0 || !return_code_list.item(0).getFirstChild().getNodeValue().equals("SUCCESS"))     return JoinServiceBase.E_UNKNOWN_ERROR;
    check_return_code=false;
  }
 else {
    return JoinServiceBase.E_MOBILE_NOT_SUPPORTED;
  }
  NodeList meetings_node=doc.getElementsByTagName("meeting");
  if (meetings_node != null) {
    for (int i=0; i < meetings_node.getLength(); ++i) {
      Meeting meeting=new Meeting();
      if (meeting.parse((Element)meetings_node.item(i),check_return_code))       meetings.add(meeting);
    }
  }
  return JoinServiceBase.E_OK;
}
 

Example 22

From project Aion-Extreme, under directory /AE-go_GameServer/src/com/aionemu/gameserver/model/quests/.

Source file: OperationFactory.java

  15 
vote

public static QuestOperation newOperation(String name,Node node) throws QuestEngineException {
  try {
    if (operations.containsKey(name)) {
      Constructor con=operations.get(name).getConstructor(new Class<?>[]{Node.class});
      return (QuestOperation)con.newInstance(new Object[]{node});
    }
  }
 catch (  InstantiationException ex) {
    throw new QuestEngineException("Error on QuestOperation \"" + name + "\".",ex);
  }
catch (  IllegalAccessException ex) {
    throw new QuestEngineException("Error on QuestOperation \"" + name + "\".",ex);
  }
catch (  IllegalArgumentException ex) {
    throw new QuestEngineException("Error on QuestOperation \"" + name + "\".",ex);
  }
catch (  InvocationTargetException ex) {
    throw new QuestEngineException("Error on QuestOperation \"" + name + "\".",ex);
  }
catch (  NoSuchMethodException ex) {
    throw new QuestEngineException("Error on QuestOperation \"" + name + "\".",ex);
  }
catch (  SecurityException ex) {
    throw new QuestEngineException("Error on QuestOperation \"" + name + "\".",ex);
  }
  throw new QuestEngineException("Unhandled QuestOperation \"" + name + "\".");
}
 

Example 23

From project big-data-plugin, under directory /shims/api/src/org/pentaho/hbase/shim/api/.

Source file: ColumnFilter.java

  15 
vote

public static ColumnFilter getFilter(Node filterNode){
  String alias=XMLHandler.getTagValue(filterNode,"alias");
  ColumnFilter returnVal=new ColumnFilter(alias);
  String type=XMLHandler.getTagValue(filterNode,"type");
  returnVal.setFieldType(type);
  String comp=XMLHandler.getTagValue(filterNode,"comparison_opp");
  returnVal.setComparisonOperator(stringToOpp(comp));
  String signed=XMLHandler.getTagValue(filterNode,"signed_comp");
  returnVal.setSignedComparison(signed.equalsIgnoreCase("Y"));
  String constant=XMLHandler.getTagValue(filterNode,"constant");
  returnVal.setConstant(constant);
  String format=XMLHandler.getTagValue(filterNode,"format");
  returnVal.setFormat(format);
  return returnVal;
}