Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删除XML标记但不删除数据_Java_Xml - Fatal编程技术网

Java 删除XML标记但不删除数据

Java 删除XML标记但不删除数据,java,xml,Java,Xml,我在Java中有一个XML字符串,如下所示 <priceFactor> <client>string</client> ... <priceFactorID> <exchangeCode>fgdf</exchangeCode> <productCode>dfg</productCode> <secType>dgf<

我在Java中有一个XML字符串,如下所示

<priceFactor>
    <client>string</client>
   ...
    <priceFactorID>
        <exchangeCode>fgdf</exchangeCode>
        <productCode>dfg</productCode>
        <secType>dgf</secType>
    </priceFactorID>

</priceFactor>

一串
...
fgdf
dfg
dgf
我想删除priceFactorID标记,但不删除子元素。所以,我想要这样的东西:

<priceFactor>
    <client>string</client>
   ...

        <exchangeCode>fgdf</exchangeCode>
        <productCode>dfg</productCode>
        <secType>dgf</secType>

</priceFactor>

一串
...
fgdf
dfg
dgf
我可以尝试字符串操作,但我想知道Java中是否有更有效的方法来操作XML;
    NodeList neighbors = priceFactorNode.getChildNodes();
    Node unwantedNode = neighbors.item(1);
    NodeList children = unwantedNode.getChildNodes();
    priceFactorNode.removeChild(unwantedNode);
    for(int x = 0; x < children.getLength(); x++) {
        priceFactorNode.appendChild(children.item(x));
    }
节点unwantedNode=邻居。项(1); NodeList childrends=unwantedNode.getChildNodes(); priceFactorNode.removeChild(unwantedNode); 对于(int x=0;x 其中,priceFactorNode隐含为标记PriceFactor的节点。这应该在抓取其子节点后删除您不想要的节点,然后将子节点附加到已删除节点的父节点,实质上是“删除”节点。您可能需要更改代码以获得所需的结果

NodeList neights=priceFactorNode.getChildNodes();
节点unwantedNode=邻居。项(1);
NodeList childrends=unwantedNode.getChildNodes();
priceFactorNode.removeChild(unwantedNode);
对于(int x=0;x

其中,priceFactorNode隐含为标记PriceFactor的节点。这应该在抓取其子节点后删除您不想要的节点,然后将子节点附加到已删除节点的父节点,实质上是“删除”节点。您可能需要更改代码以获得所需的结果

也许是合适的?你不能简单地移除一个节点,无论是在视觉上还是在ui中,你都可以做到。实际上,您必须解析、形成元素并再次写入,以便能够看到新的结构。其他人可以对此发表评论。也许这是合适的?你不能简单地删除一个节点,无论是在视觉上还是在ui中,你都可以这样做。实际上,您必须解析、形成元素并再次写入,以便能够看到新的结构。其他人可以对此发表评论。