如何从java中的xml中提取某些块?

时间:2015-10-20 11:06:52

标签: java xml xpath

我有以下xml文件。

<?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>

我想仅使用createTable提取块,使用xpath将其解压缩到其他xml文件。结果应如下所示:

<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>

我现在所做的代码如下:

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());


}
}

我并不清楚如何遍历xpath并提取所需的块。

2 个答案:

答案 0 :(得分:1)

您需要使用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;
}

答案 1 :(得分:0)

这将返回阻止所有createTable块。

//changeSet[createTable]

/databaseChangeLog/changeSet[createTable]

在这里,您要求返回存在createTable的changeSet块。

结果

<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 />
</changeSet>