Java 删除XML标记但不删除数据
我在Java中有一个XML字符串,如下所示Java 删除XML标记但不删除数据,java,xml,Java,Xml,我在Java中有一个XML字符串,如下所示 <priceFactor> <client>string</client> ... <priceFactorID> <exchangeCode>fgdf</exchangeCode> <productCode>dfg</productCode> <secType>dgf<
<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;xNodeList neights=priceFactorNode.getChildNodes();
节点unwantedNode=邻居。项(1);
NodeList childrends=unwantedNode.getChildNodes();
priceFactorNode.removeChild(unwantedNode);
对于(int x=0;x
其中,priceFactorNode隐含为标记PriceFactor的节点。这应该在抓取其子节点后删除您不想要的节点,然后将子节点附加到已删除节点的父节点,实质上是“删除”节点。您可能需要更改代码以获得所需的结果 也许是合适的?你不能简单地移除一个节点,无论是在视觉上还是在ui中,你都可以做到。实际上,您必须解析、形成元素并再次写入,以便能够看到新的结构。其他人可以对此发表评论。也许这是合适的?你不能简单地删除一个节点,无论是在视觉上还是在ui中,你都可以这样做。实际上,您必须解析、形成元素并再次写入,以便能够看到新的结构。其他人可以对此发表评论。