Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract certain blocks from xml in java?

Tags:

java

xml

xpath

I have the following xml file.

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog>
   <changeSet author="debdipta.h (generated)" id="1442927616936-399">
      <createTable tableName="SI_FORWARDING_EVENT_HISTORY_AU">
         <column name="FORWARDING_ID_O" type="NUMBER" />
         <column name="FORWARDING_ID_N" type="NUMBER" />
         <column name="ASSIGNMENT_ID_O" type="NUMBER" />
         <column name="ASSIGNMENT_ID_N" type="NUMBER" />
         <column name="PROCESS_INSTANCE_ID_O" type="NUMBER" />
         <column name="PROCESS_INSTANCE_ID_N" type="NUMBER" />
         <column name="ORIGINAL_USER_ID_O" type="NUMBER" />
         <column name="ORIGINAL_USER_ID_N" type="NUMBER" />
         <column name="FORWARD_FROM_USER_O" type="NUMBER" />
         <column name="FORWARD_FROM_USER_N" type="NUMBER" />
         <column name="FORWARD_TO_USER_O" type="NUMBER" />
         <column name="FORWARD_TO_USER_N" type="NUMBER" />
         <column name="COMMENTS_O" type="VARCHAR2(4000 CHAR)" />
         <column name="COMMENTS_N" type="VARCHAR2(4000 CHAR)" />
         <column name="XML_O" type="VARCHAR2(4000 CHAR)" />
         <column name="XML_N" type="VARCHAR2(4000 CHAR)" />
         <column name="CREATED_BY_O" type="NUMBER" />
         <column name="CREATED_BY_N" type="NUMBER" />
         <column name="CREATION_DATE_O" type="date" />
         <column name="CREATION_DATE_N" type="date" />
         <column name="AUDIT_TRAIL_REASON" type="VARCHAR2(1000 CHAR)" />
         <column name="TRANSACTION_TYPE" type="VARCHAR2(100 CHAR)" />
      </createTable>
   </changeSet>

   <changeSet author="deb.h" id="4588999797">
      <createView />
   </changeSet>

   <changeSet author="deb.h" id="4588999797">
      <createTable />
   </changeSet>

   <changeSet author="deb.h" id="4588999797">
      <createView />
   </changeSet>
</databaseChangeLog>

I want to extract the blocks only with createTable to be extracted to some other xml file using xpath. The result should look like:

<databaseChangeLog>
<changeSet author="debdipta.h (generated)" id="1442927616936-399">
        <createTable tableName="SI_FORWARDING_EVENT_HISTORY_AU">
            <column name="FORWARDING_ID_O" type="NUMBER"/>
            <column name="FORWARDING_ID_N" type="NUMBER"/>
            <column name="ASSIGNMENT_ID_O" type="NUMBER"/>
            <column name="ASSIGNMENT_ID_N" type="NUMBER"/>
            <column name="PROCESS_INSTANCE_ID_O" type="NUMBER"/>
            <column name="PROCESS_INSTANCE_ID_N" type="NUMBER"/>
            <column name="ORIGINAL_USER_ID_O" type="NUMBER"/>
            <column name="ORIGINAL_USER_ID_N" type="NUMBER"/>
            <column name="FORWARD_FROM_USER_O" type="NUMBER"/>
            <column name="FORWARD_FROM_USER_N" type="NUMBER"/>
            <column name="FORWARD_TO_USER_O" type="NUMBER"/>
            <column name="FORWARD_TO_USER_N" type="NUMBER"/>
            <column name="COMMENTS_O" type="VARCHAR2(4000 CHAR)"/>
            <column name="COMMENTS_N" type="VARCHAR2(4000 CHAR)"/>
            <column name="XML_O" type="VARCHAR2(4000 CHAR)"/>
            <column name="XML_N" type="VARCHAR2(4000 CHAR)"/>
            <column name="CREATED_BY_O" type="NUMBER"/>
            <column name="CREATED_BY_N" type="NUMBER"/>
            <column name="CREATION_DATE_O" type="date"/>
            <column name="CREATION_DATE_N" type="date"/>
            <column name="AUDIT_TRAIL_REASON" type="VARCHAR2(1000 CHAR)"/>
            <column name="TRANSACTION_TYPE" type="VARCHAR2(100 CHAR)"/>
        </createTable>
</changeSet>

<changeSet author="deb.h" id="4588999797">
        <createTable></createTable>
</changeSet>
</databaseChangeLog>

The code I have done still now is as follows:

public class extractXML {
    public static void main(String[] args)throws Exception{
        //String test=readXMLasString.convert("E:\\db.borilog.xml");
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        DocumentBuilder db=dbf.newDocumentBuilder();
        Document doc=db.parse(new FileInputStream("E:\\db.borilog.xml"));
        XPath xpath=XPathFactory.newInstance().newXPath();
        Node res=(Node) xpath.evaluate("databaseChangeLog/changeSet",doc,XPathConstants.NODE);
        System.out.println(NodeToString(res));
    }

public static String NodeToString(Node node)throws TransformerException{
    StringWriter buf=new StringWriter();
    Transformer xform=TransformerFactory.newInstance().newTransformer();
    xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    xform.transform(new DOMSource(node),new StreamResult(buf));
    return(buf.toString());


}
}

I am not undrstanding how to loop through the xpath and extract the required blocks.

like image 821
Debdipta Halder Avatar asked Nov 09 '22 03:11

Debdipta Halder


1 Answers

You need to use NodeList:

NodeList nodeList = (NodeList)xpath.evaluate("//changeSet[createTable]",doc,XPathConstants.NODE);
String[] results = new String[nodeList.getLength()];
for (int index = 0; index < nodeList.getLength(); index++) {
    Node node = nodeList.item(index);
    String nodeAsString = NodeToString(node);
    results[index] = nodeAsString;
}
like image 70
Andriy Kuba Avatar answered Nov 14 '22 22:11

Andriy Kuba