Xpath插入节点下的节点序列 - Java

时间:2015-12-04 19:54:49

标签: java dom xpath xml-parsing xpath-2.0

我正在尝试在节点下添加节点序列,下面是我的参考xml:



<?xml version="1.0" encoding="UTF-8"?>
<transformation>
  <info>
    <name>Bulkload</name>
    <description/>
    <extended_description/>
    <trans_version/>
    <trans_type>Normal</trans_type>
    <trans_status>0</trans_status>
    <directory>&#x2f;</directory>
  </info>
  <connection>
    <name>con_name</name>
    <server>server</server>
    <type>SYBASE</type>
    <access>Native</access>
    <database>database</database>
    <port>port</port>
    <username>user</username>
    <password>Encrypted xyz</password>
  
  </connection>

  <step>
    <name>Extract</name>
    <type>TextFileOutput</type>
    <fields>

**HERE**
<field>
</field>
	</fields>
     <cluster_schema/>
 <remotesteps>   <input>   </input>   <output>   </output> </remotesteps>    <GUI>
      <xloc>300</xloc>
      <yloc>168</yloc>
      <draw>Y</draw>
      </GUI>
    </step>
</transformation>
&#13;
&#13;
&#13;

我想在字段标记内添加以下xml块:

&#13;
&#13;
<field>
            <name>field_name</name>
            <type>Integer</type>
            <format>&#x23;&#x3b;-&#x23;</format>
            <currency/>
            <decimal>.</decimal>
            <group>,</group>
            <nullif/>
            <trim_type>none</trim_type>
            <length>9</length>
            <precision>0</precision>
</field>
&#13;
&#13;
&#13;

我的要求是从上面的模板创建新的xml文档。我只在Xpath中找到一个方法在节点之前插入节点,所以我更新了我的模板并在 fields 节点中添加了一个空白字段节点,并且我能够插入一个带有以下代码的节点:

        File dest =new File("H:\\Project_Documents\\reference.ktr");
        DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            factory.setIgnoringComments(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            String newLine = System.getProperty("line.separator");
            Document document = builder.parse(dest);
            XPathFactory xpathFactory = XPathFactory.newInstance();
            XPath xpath = xpathFactory.newXPath();

            NodeList nodes = document.getElementsByTagName("field");
            Text a = document.createTextNode("Anup");
            Element p =document.createElement("field");
            p.appendChild(a);
nodes.item(0).getParentNode().insertBefore(p, nodes.item(0));

但我再次面对儿童节点的名称,类型,格式等问题。有人能告诉我一种实现目的的方法吗?

如果我无法使用Xpath实现它,那么我应该使用哪个解析器?

1 个答案:

答案 0 :(得分:1)

一种有效的解决方案:

您必须管理两个XML文档,找到所需的标记并替换。

使用XPath,您可以更精确地选择,但不能在那里使用。

1获取全局XML的DOM

Document document= ...

2获取要插入的节点

Document document_to_insert = builder.parse(new InputSource(new StringReader(xml_to_insert))); // ...

//GET NODE field
NodeList nodes_field_to_insert=document_to_insert.getElementsByTagName("field");

Element node_field_to_insert=null;

for(int i=0; i<nodes_field_to_insert.getLength(); i++)
{
 Node the_node = nodes_field_to_insert.item(i);

 // WE TAKE THE FIRST ONE
 if(the_node instanceof Element)
    {
     node_field_to_insert=(Element) the_node;
     break;
    }
}

3转到全局XML

中的字段节点

4替换节点内部

// GET NODE field
NodeList nodes=document.getElementsByTagName("field");

for(int i=0; i<nodes.getLength(); i++)
{
   Node the_node = nodes.item(i);

   if(the_node instanceof Element)
     {
     Element a_child = (Element) the_node;
     Node newNode = document.importNode(node_field_to_insert, true);

     // FATHER
     Node the_parent=a_child.getParentNode();
     the_parent.replaceChild(newNode,a_child);

     // WE STOP
     break;
 }
}