Monday, 21 September 2020

How to get cdata value from xml in java

How to get cdata value from xml in java  | How to read cdata in xml using java


In this article, we are going to discuss XML CDATA which stands for Character Data. which is defined as blocks of text that will not be parsed by the xml parser.

 syntax of cdata in xml:


    <![CDATA[
        characters with markup
        ]]>


Java provides some of the options to parse XML documents. Some of the commonly used  XML parsers for java programming language are as follows:

  1.     DOM Parser  (Document Object Model)
  2.     SAX Parser
  3.     StAX Parser  (Streaming API for XML)
  4.     JAXB

    
let us consider the following xml file cdata.xml

cdata.xml


<?xml version="1.0" encoding="UTF-8"?>
<employee>
  <emp_info id="1">
    <emp_name>
      <first_name>abc</first_name>
      <last_name>xyz</last_name>
    </emp_name>
    <emp_contact_info>
      <address><![CDATA[
        This is the CDATA area.
        You can store special character that are recognize. Ex. '  " & / - < >
        you can specify company address also
        ]]>
      </address>
    </emp_contact_info>
  </emp_info>
</employee>

This is the java program.

XmlCData.java

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

class XmlCData{
    public static String getCharacterDataFromElement(Element e) {
        NodeList list = e.getChildNodes();
        String data;

        for(int index = 0; index < list.getLength(); index++){
            if(list.item(index) instanceof CharacterData){
                CharacterData child = (CharacterData) list.item(index);
                data = child.getData();

            if(data != null && data.trim().length() > 0)
                return child.getData();
        }
    }
    return "";
  }
}


JavaXmlCdata .java


public class JavaXmlCdata {    
    public static void main(String[] args) throws Exception{
        File file = new File("D:\\cdata.xml");
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(file);
        
        NodeList nodes = doc.getElementsByTagName("emp_contact_info");
        
        for (int i = 0; i < nodes.getLength(); i++) {
      
            Element element = (Element) nodes.item(i);
            NodeList title = element.getElementsByTagName("address");
            Element line = (Element) title.item(0);
            System.out.println("Cdata is  " + XmlCData.getCharacterDataFromElement(line));
    }
  }
}


Monday, 7 September 2020

Write a note on compiling and executing a Java program

 Write a note on compiling and executing a Java program


First, we have to open a text file using notepad or any text editor and then write the java program and then save this java program by any name with extention java example <anyname.java>.
Now compile the java program and then run the java program.

Compilation process of java using command prompt:

  •    open command prompt
  •    goto the place where java program is saved.
  •    now type below command

                d:\> javac   <filename>.java


If no compilation errors are there then we get .class file which consists of bytecode.
Once after sucess compilation we have to run the java program

Process of running the java program using command prompt:

  •      d:\> java <file name>

    
     name of the file which consists of main method and main method should be public static void main(String args[]).
 This bytecode gets interpreted on different machines.

Java virtual machine (JVM) is responsible for allocating memory space.

Friday, 4 September 2020

The add values of matrix horizontally with another vertically

 Java program which will add the values of 2d matrix horizontally with another 2d matrix vertically

import java.util.*;
class MatrixAddition{
    public static void main(String args[]){
    int matrix1[][]=new int[2][2];
    int matrix2[][]=new int[2][2];
    int res_matrix[][]=new int[2][2];
    int i, j;
   Scanner sc=new Scanner(System.in);

    // reading the elements of first matrix
    System.out.println(" enter elements of first matrix ");
    for( i=0; i<2 ;i++){
        for(j=0;j<2;j++){
            matrix1[i][j]=sc.nextInt();
        }
    }

    // reading the elements of second matrix
    System.out.println(" enter the elements of second matrix ");
    for( i=0; i<2; i++){
        for(j=0; j<2; j++){
            matrix2[i][j]=sc.nextInt();
        }
    }

    // adding two  matrices`
    for( i=0; i<2; i++){
        for (j=0; j<2; j++){
            res_matrix[i][j]= matrix1[i][j] + matrix2[j][i];
        }
    }

    // printing the resultant matrix  
    System.out.println(" the resultant matrix is  \n");
    for (i=0; i<2; i++){
        for( j=0; j<2; j++){
            System.out.print(res_matrix[i][j]+" ");
        }
        System.out.println("");
    }
  }
}

Wednesday, 2 September 2020

Java program for finding double letter sequence word in Java

 

 

 

 

Java program for finding double letter sequence word in Java

Double-letter words:

   These are the words which contain atleast one set of characters used twice consecutively.

 For example:
    add,all, bee, boo, ebb, ell, egg, fee, goo, tee, too,  see.


import java.util.*;
class DoubleSequenceWord{
    public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    System.out.println("enter a word to check whether it is double sequence or not");
    String str=sc.nextLine();
    int count=0;
        for(int i=0;i<str.length()-1;i++){
            if(str.charAt(i)==str.charAt(i+1)){
                count++;
                break;
            }
        }
        if(count>0)
            System.out.println("It is a double sequence word");
        else
            System.out.println("It is a not a double sequence word");

   }

}