簡體   English   中英

剝離一些xml文檔的標簽

[英]stripping of some tags of xml document

我有一個xml doc如下。

<data>
     <employee>
       <moretag> may or may not contain inner tag and attributes</moretag>
       <somemore>  may contain inner tags</somemore>
     </employee>
</data>

我想要如下操作

 <employee>
       <moretag> may or may not contain inner tag and attributes</moretag>
       <somemore>  may contain inner tags</somemore>
  </employee>

那就是我要剝離data tags我怎么能這樣做?

你可以使用jdom

InputStream is = new FileInputStream(xmlFileName);
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
Document doc = new SAXBuilder().build(isr);

Element data = doc.getRootElement();
Element employee = data.getChild("employee");

XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
xmlOut.output(employee, System.out);

用XSLT做

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="data">
    <xsl:copy-of select="child::node()" />
  </xsl:template>
</xsl:stylesheet>

你不能簡單地剝離這樣的節點。

您傾向於實現的簡單邏輯是您創建根節點的firstChild的副本,即用它替換根節點。

PS,根節點下只有一個孩子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM