2011-06-21 6 views
0

私は以下のXML文書を持っています。XML文書の一部のタグを取り除く

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

私はdata tags .Howを取り除くしたいされ

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

以下のようにopが、私はそれを行うことができますしたいですか?

+0

でそれを行うあなたがSAXパーサ対DOMへの好みを持っていますか? 純粋なテキストやオブジェクトとして必要ですか? – RonK

+0

テキスト、私はdomを好む – akshay

+0

Thorの答えがあなたの必要とするもののように見えます。 – RonK

答えて

1

あなたはこのために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); 
0

あなたは単にそのようないくつかのノードを取り除くことはできません。

あなたが達成する傾向にあるもののシンプルなロジックを使用すると、ルートノードののfirstChild、すなわちのコピーを作成し、それを持つルートノードを交換することです。

P.S.ルートノードの下に子が1つしかないことを確認します。

1

は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> 
関連する問題