选择xml原始文本

给出像这样的xml:

     Text      

我想选择item / xmlText下的所有文本 。 我想标签(someTag,otherTag)打印此节点的所有内容。

我更愿意用XPath处理这个问题,但这是Java程序的一部分,所以如果有这样的机制我也可以接受它。

使用XSLT

        

当这应用于提供的XML文档 (更正为格式良好!!!):

      Text      

产生了想要的正确结果

   Text   

这是使用XPath检索的元素

 XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); Element element = (Element) xpath.evaluate( "/container/item/xmlText", document, XPathConstants.NODE); 

然后,你可以沿着这些方向做点什么:

 java.io.ByteArrayOutputStream data = new java.io.ByteArrayOutputStream(); java.io.PrintStream ps = new java.io.PrintStream(data); // These classes are part of Xerces. But you will find them in your JDK, // as well, in a different package. Use any encoding here: org.apache.xml.serialize.OutputFormat of = new org.apache.xml.serialize.OutputFormat("XML", "ISO-8859-1", true); org.apache.xml.serialize.XMLSerializer serializer = new org.apache.xml.serialize.XMLSerializer(ps, of); // Here, serialize the element that you obtained using your XPath expression. serializer.asDOMSerializer(); serializer.serialize(element); // The output stream now holds serialized XML data, including tags/attributes... return data.toString(); 

UPDATE

这将更简洁,而不是使用Xerces内部。 它与Dimitre的解决方案相同,只是没有使用XSLT样式表,而是全部使用Java:

 ByteArrayOutputStream out = new ByteArrayOutputStream(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); Source source = new DOMSource(element); Result target = new StreamResult(out); transformer.transform(source, target); return out.toString();