How to compare after parsing xml file

Hi,
following code, parse the input.xml file, counts how many nodes are there and writes the node name and its value on screen.
1) i am having trouble writing only node name into another file instead of writing to screen.
2) after parsing, i like to compare each node name with another .xsd file for existence.
Please keep in mind that, input.xml is based on some other .xsd and after parsing i have comparing its tag with another .xsd
Need you help guys.
thanks
* CompareTags.java
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
/** This class represents short example how to parse XML file,
* get XML nodes values and its values.<br><br>
* It implements method to save XML document to XML file too
public class CompareTags {
private final static String xmlFileName = "C:/input.xml";
     int totalelements = 0;
/** Creates a new instance of ParseXMLFile */
public CompareTags() {
// parse XML file -> XML document will be build
Document doc = parseFile(xmlFileName);
// get root node of xml tree structure
Node root = doc.getDocumentElement();
// write node and its child nodes into System.out
System.out.println("Statemend of XML document...");
writeDocumentToOutput(root,0);
               System.out.println("totalelements in xyz tag " + totalelements);
          System.out.println("... end of statement");
/** Returns element value
* @param elem element (it is XML tag)
* @return Element value otherwise empty String
public final static String getElementValue( Node elem ) {
Node kid;
if( elem != null){
if (elem.hasChildNodes()){
for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
if( kid.getNodeType() == Node.TEXT_NODE ){
return kid.getNodeValue();
return "";
private String getIndentSpaces(int indent) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < indent; i++) {
buffer.append(" ");
return buffer.toString();
/** Writes node and all child nodes into System.out
* @param node XML node from from XML tree wrom which will output statement start
* @param indent number of spaces used to indent output
public void writeDocumentToOutput(Node node,int indent) {
// get element name
String nodeName = node.getNodeName();
// get element value
String nodeValue = getElementValue(node);
// get attributes of element
NamedNodeMap attributes = node.getAttributes();
System.out.println(getIndentSpaces(indent) + "NodeName: " + nodeName + ", NodeValue: " + nodeValue);
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
System.out.println(getIndentSpaces(indent + 2) + "AttributeName: " + attribute.getNodeName() + ", attributeValue: " + attribute.getNodeValue());
// write all child nodes recursively
NodeList children = node.getChildNodes();
          //int totalelements = 0;
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
               //     System.out.println("child value.."+child);
if (child.getNodeType() == Node.ELEMENT_NODE) {
writeDocumentToOutput(child,indent + 2);
                         if(node.getNodeName() == "DATA"){
                         totalelements = totalelements+1;}
                    //System.out.println("totalelements in DATA tag " + totalelements);
/** Parses XML file and returns XML document.
* @param fileName XML file to parse
* @return XML document or <B>null</B> if error occured
public Document parseFile(String fileName) {
System.out.println("Parsing XML file... " + fileName);
DocumentBuilder docBuilder;
Document doc = null;
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setIgnoringElementContentWhitespace(true);
try {
docBuilder = docBuilderFactory.newDocumentBuilder();
catch (ParserConfigurationException e) {
System.out.println("Wrong parser configuration: " + e.getMessage());
return null;
File sourceFile = new File(fileName);
try {
doc = docBuilder.parse(sourceFile);
catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
catch (IOException e) {
System.out.println("Could not read source file: " + e.getMessage());
System.out.println("XML file parsed");
return doc;
/** Starts XML parsing example
* @param args the command line arguments
public static void main(String[] args) {
new CompareTags();
}

hi,
check out the following links
Check this blog to extract from XML:
/people/kamaljeet.kharbanda/blog/2005/09/16/xi-bi-integration
http://help.sap.com/saphelp_nw04/helpdata/en/fe/65d03b3f34d172e10000000a11402f/frameset.htm
Check thi link for Extract from any DB:
http://help.sap.com/saphelp_nw04s/helpdata/en/58/54f9c1562d104c9465dabd816f3f24/content.htm
regards
harikrishna N

Similar Messages

  • How to compare two huge xml files(50MB+) using Java Code

    I want to compare two huge xml files using java code and need to find the difference of those xml files
    is there any API for that

    You should find third party API

  • How to generate GUI code from parsed XML file?

    hai,
    I have to generate GUI code after parsing any configuration XML file as input.Now i have parsed my XML file and got its attributed and i want to get some idea of how to map the parsed XML attributes to the java code to build GUI code.

    Hello,
    1. I like to create data type from a XML file structure, which contains the data structure ?
    XML fields will need to be taken note of to see which is repeating or not. You can also load the XML into a third-party tool such as Altova XML Spy and then generate an XSD from there. You will need to import the XSDs into PI under external definitions. However, this does not guarantee business interoperability, as such, it is always best to ask the provider for the XSDs or WSDL. It will also save you a lot of time from guessing which fields are needed or not.
    2. How to create custom node function in graphical mapping editor ?
    In your graphical mapping editor, on the lowest left corner, there is an icon there that says Create New Function. You must take into account their return types:
    1. Single Values = String
    2. Queue/Context (no return type) thus resultList is used.
    Hope this helps,
    Mark

  • How to parse XML files from normal FTP Servers?

    I want to parse xml files from a normal FTP Servers , NOT the sap application severs itself. How can i do that?
    I know how to use the SAPFTP getting and putting files ,but I don't want to download and then parse it.
    Who knows how to parse it directly? I Just need to read the contents into a database.
    Thanks.

    I want to parse xml files from a normal FTP Servers , NOT the sap application severs itself. How can i do that?
    I know how to use the SAPFTP getting and putting files ,but I don't want to download and then parse it.
    Who knows how to parse it directly? I Just need to read the contents into a database.
    Thanks.

  • How do I create individual xml files from the parsed data output of a xml file?

    I have written a program (DOM Parser) that parses data from a XMl File. I would like to create an individual file with the corresponding name for each set of data parsed from the xml document. If the parsed output is Single, Double, Triple, I would like to create an individual xml file (Single.xml, Double.xml, Triple.xml)with those corresponding names. How do I create the xml files and give each file the name of my parsed data output? Thanks in advance for your help.
    import java.io.IOException;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    public class MyDomParser {
      public static void main(String[] args) {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      try {
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document doc = builder.parse("ENtemplate.xml");
      doc.normalize();
      NodeList rootNodes = doc.getElementsByTagName("templates");
      Node rootNode = rootNodes.item(0);
      Element rootElement = (Element) rootNode;
      NodeList templateList = rootElement.getElementsByTagName("template");
      for(int i=0; i < templateList.getLength(); i++) {
      Node theTemplate = templateList.item(i);
      Element templateElement = (Element) theTemplate;
      System.out.println("Template" + ": " +templateElement.getAttribute("name")+ ".xml");
      } catch (ParserConfigurationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (SAXException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();

    Ive posted the new code but now I'm getting a FileAlreadyExistException error. How do I handle this exception error correctly in my code?
    import java.io.IOException;
    import java.nio.file.FileAlreadyExistsException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    public class MyDomParser {
      public static void main(String[] args) {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      try {
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document doc = builder.parse("ENtemplate.xml");
      doc.normalize();
      NodeList rootNodes = doc.getElementsByTagName("templates");
      Node rootNode = rootNodes.item(0);
      Element rootElement = (Element) rootNode;
      NodeList templateList = rootElement.getElementsByTagName("template");
      for(int i=0; i < templateList.getLength(); i++) {
      Node theTemplate = templateList.item(i);
      Element templateElement = (Element) theTemplate;
      System.out.println(templateElement.getAttribute("name")+ ".xml");
      for(int i=0; i < templateList.getLength(); i++) {
      Node theTemplate = templateList.item(i);
      Element templateElement = (Element) theTemplate;
      String fileName = templateElement.getAttribute("name") + ".xml";
      Files.createFile(Paths.get(fileName));
      System.out.println("File" + ":" + fileName + ".xml created");
      } catch (ParserConfigurationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (SAXException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();

  • How to parse xml file in midlet

    Hi Guys,
    i wish to parse xml file and display it in my midlet. i found api's supporting xml parsing in j2se ie., in java.net or j2se 5.0. Can u please help me what package to use in midlet?
    how to parse xml info and display in midlet? Plz reply soon......Thanks in advance....

    i have exactly the same problem with KXml2, but using a j2me-polish netbeans project.
    i've tried to work around with similar ways like you, but none of them worked. now i've spent 3 days for solving this problem, i'm a bit disappointed :( what is wrong with setting the downloaded kxml2 jar path in libraries&resources?
    screenshot

  • How to stop parse XML file

    When using saxParser.parse(XMLfile, handler) to parse XML file, How to stop the parsing but not exit. I catched thread interrupted in startElement(), but can not stop it because it still go through all other startElement()s and endElement()s. Is there any method or class can stop parse XML?
    Appreciate your help!
    Edward

    Please look at the technote:
    http://access1.sun.com/technotes/01185.html
    Hope this helps.
    Michelle Cope
    Sun Microsystems.

  • Parsing XML Files to ABAP

    Hello All,
    There is a requirement for parsing of XML files to ABAP.
    1.How do we pick an XML file from Application server and also from FTP server?
    2.After picking the XML file how to parse that XML file to process it to create material master?

    Hi,
    Ur scenario is File to R/3
    For creating material master ..i guess there is IDoc named MATMAS.
    U can make use of it and execute File to IDoc Scenario.
    link for File to IDoc--
    https://www.sdn.sap.com/irj/scn/wiki?path=/display/xi/fileToIDOC&
    U need to pick XML file from FTP server...No need to parse XML file...just create data type which represents ur xml file structure and map it to IDoc fields.
    regards,
    Manisha

  • How to retrieve value from xml file

    hi all,
    can somebody pls tell me how to retrieve value from xml file using SAXParser.
    I want to retrieve value of only one tag and have to perform some validation with that value.
    it's urgent .
    pls help me out
    thnx in adv.
    ritu

    hi shanu,
    the pbm is solved, now i m able to access XXX no. in action class & i m able to validate it. The only thing which i want to know is it ok to declare static ArrayList as i have done in this code. i mean will it affect the performance or functionality of the system.
    pls have a look at the following code snippet.
    public class XMLValidator {
    static ArrayList strXXX = new ArrayList();
    public void validate(){
    factory.setValidating(true);
    parser = factory.newSAXParser();
    //all factory code is here only
    parser.parse(xmlURI, new XMLErrorHandler());     
    public void setXXX(String pstrXXX){          
    strUpn.add(pstrXXX);
    public ArrayList getXXX(){
    return strXXX;
    class XMLErrorHandler extends DefaultHandler {
    String tagName = "";
    String tagValue = "";
    String applicationRefNo = "";
    String XXXValue ="";
    String XXXNo = "";          
    XMLValidator objXmlValidator = new XMLValidator();
    public void startElement(String uri, String name, String qName, Attributes atts) {
    tagName = qName;
    public void characters(char ch[], int start, int length) {
    if ("Reference".equals(tagName)) {
    tagValue = new String(ch, start, length).trim();
    if (tagValue.length() > 0) {
    RefNo = new String(ch, start, length);
    if ("XXX".equals(tagName)) {
    XXXValue = new String(ch, start, length).trim();
    if (XXXValue.length() > 0) {
    XXXNo = new String(ch, start, length);
    public void endElement(String uri, String localName, String qName) throws SAXException {                    
    if(qName.equalsIgnoreCase("XXX")) {     
    objXmlValidator.setXXX(XXXNo);
    thnx & Regards,
    ritu

  • How To check validity of XML File

    can any one tell me how to find whether an XML file is valid or not before beginning to parse the XML file
    Thanks in advance

    Xerces will do that for qou, if you activate validation (and, of course, a DTD is specified in the file). Read the documentation of Xerces at xml.apache.org

  • Parse XML files

    Hi
    Anyone know about support for parsing XML files in LabVIEW?
    (I mean specific XML support, I'm familiar with LabVIEWs file functions)
    regards
    Jan
    Sent via Deja.com http://www.deja.com/
    Before you buy.

    I assume you are referring to:
    http://www.savarese.org/oro/software/OROMatcher1.1.html
    Have you considered asking Savarese?
    Peace,
    Cameron Purdy
    Tangosol, Inc.
    http://www.tangosol.com
    +1.617.623.5782
    WebLogic Consulting Available
    "Laurent Mentek" <[email protected]> wrote in message
    news:[email protected]..
    Hi all,
    I'm started to develop with BEA WebLogic and I use ORACLE 8.1.6
    database.
    We need to map some XML tags as metadata in the database.
    Here is a concrete example with part of our XML files:
    XML files :
    <target>EDU</target>
    <question>
    <para>
    Please could you provide some references on nutritional status in
    the frail elderly?
    </para>
    </question>
    I use OROMatcher to parse xml files and it work fine.
    I can extract every element in line with success , but don't extract the
    value in <para> tag, for example.
    I don't no how to use the MULTILINE_MASK option and the ^ or $ to get
    this line.
    Anyone could give me an example of metadatas extaction using or no the
    MULTILINE_MASK option?
    Thanks a lot for your help.
    Laurent.

  • How I can create a XML file from java Aplication

    How I can create a XML file from java Aplication
    whith have a the following structure
    <users>
    <user>
    <login>anyName</login>     
    <password>xxxx</password>
    </user>
    </users>
    the password label must be encripted
    accept any suggestion

    Let us assume you have all the data from the jsp form in an java bean object..
    Now you want a xml file. This can be acheived in 2 ways
    1. Write it into a file using java.io classes. Say you have a class with name
    write("<name>"+obj.getName+</name>);
    bingo you have a flat file with the xml
    2. Use data binding to do the trick
    will recommend JiBx and Castor for the 2nd option
    Regards,
    Rajagopal

  • How to Read and Generate XML file from java code.

    hi guys,
    how to read the xml file (Condition :we know only DTD or Shema only).
    How to Generate the new xml file ?(using Shema )
    And one more how directly Generate the xml from DB?
    Pleas with code or any URL

    Using XMLbeans you can generate Java objects from an XSD schema (perhaps DTDs aswell)
    Then you can create an instance of the Document object and ask it to write itself.
    This will create an XML document complient to the schema.
    XMLBeans generates a "type" safe DOM where you can only ever have a structure compilent to you schema.
    matfud

  • How I could generate an XML file from a report in version 4.0B

    Good morning,
    How could I generate an XML file from a report? Please note that I am using version 4.0B
    I don't have access to
    Billy Vital

    Hi,
            In the Class CL_XML_DOCUMENT,
                 we have a method  EXPORT_TO_FILE to download an XML file.

  • Error in retrieving and parsing XML File

    Hi Folks
    I am Working on People centric user interface, While i am custimizing a application in Business application Builder i am getting this error
    " Error in retrieving and parsing XML File "
    can any body look on this and give me the solution
    it will be rewarded
    Regards
    M.S.Kumar

    Hello,
    As mentionned by SAP_TECH, avoid to use the BAB.
    Go to CRMC_BLUEPRINT_C and use the different option in the menu to customize the field group, toolbar group, events, ...
    Use the PCUI cookbook to find your way.
    Regards,
    Fred

Maybe you are looking for

  • Problem with contact form

    Hi- I found a contact form for my Flash website but it's not working properly. It uses ActionScript 3 and PHP, both of which I don't know much about. Because of this, I can't pinpoint the problem or how to fix it. Here's what's going on: 1) Each fiel

  • Since updating my ipad to IOS 6.0.1, when I move my apps around and press the home button to stop them shaking, it switches my ipad off and back on again

    SInce updating my ipad 2 to IOS 6.0.1, when I try to rearrange my apps and press the home button to stop them shaking it turns my ipad off, then it shows the apple sign on the screen and comes back on, but the apps are back in the places they was bef

  • Where are my iPhoto files?

    I have a basic understanding of iPhoto but am trying to learn more. Specifically I'm confused about where my actual photo files are stored. Say I have a single picture. Now when I import that picture from my camera it seems that 1 copy goes into iPho

  • Consignment stock replenishment via idoc

    We are setting up a scenario, where we receive oderCreate idocs (CIDX format) into our XI system which should trigger a stock replenishment from the producing plant into the customer consignment stock. As the delivery has to be created with reference

  • Return to default desktop, apparent "loss" of home directory

    this just recently happened to me. i remember changing the name of my hard drive. i didn't think itt would change anything. now i can'tt find anything on my computer. i tried changing it back, but i'm not totally sure if i am getting the name exactly