简体   繁体   English

是否有来自.NET的XmlDocument.LoadXml()的Java等价物?

[英]Is there a Java equivalent for XmlDocument.LoadXml() from .NET?

In .NET C#, when trying to load a string into xml , you need to use XmlDocument type from System.Xml and do the following: .NET C#中,当尝试将string加载到xml ,需要使用System.Xml XmlDocument类型并执行以下操作:

eg: 例如:

string xmlStr = "<name>Oscar</name>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlStr);
Console.Write(doc.OuterXml);

This seems simple but how can I do this in Java ? 这看起来很简单,但我怎么能用Java做到这一点? Is it possible to load a string into xml using something directly, short and simple like above and avoid implementing other methods for this? 是否可以使用直接,简短和简单的东西将string加载到xml ,并避免为此实现其他方法?

Thanks in advance. 提前致谢。

Try this: 尝试这个:

DocumentBuilderFactory documentBuildFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder doccumentBuilder = documentBuildFactory.newDocumentBuilder();
Document document = 
 doccumentBuilder.parse(new ByteArrayInputStream("<name>Oscar</name>".getBytes()));

You can traverse Oscar by: 你可以通过以下方式遍历Oscar

String nodeText = document.getChildNodes().item(0).getTextContent() ;         
System.out.println(nodeText);

To transaform back: 转换回来:

TransformerFactory tFactory =  TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
//to print the string in sysout, System.out
StreamResult streamResult = new StreamResult(System.out);
transformer.transform(domSource, streamResult ); 

To get the result in String: 要在String中获得结果:

DOMSource source = new DOMSource(document);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
StreamResult result = new StreamResult(outStream);
transformer.transform(source, result);
String resultString = new String( outStream.toByteArray());
System.out.println(resultString);

You have a choice of tree models in Java - DOM, XOM, JDOM, DOM4J. 您可以选择Java中的树模型 - DOM,XOM,JDOM,DOM4J。 Many people (including Singh above) use DOM by default because it is included in the JDK, but it's probably the worst of the bunch, largely because it's the oldest (it was invented before namespaces came along), and because it tries to do too much (HTML, event handling etc, as well as XML). 许多人(包括上面的Singh)默认使用DOM,因为它包含在JDK中,但它可能是最糟糕的,主要是因为它是最古老的(它是在命名空间出现之前发明的),并且因为它也试图做很多(HTML,事件处理等,以及XML)。 I'd suggest using JDOM2. 我建议使用JDOM2。 It shouldn't be hard if you look at the Javadoc for you to find the method that builds a JDOM2 document from an input stream. 如果您查看Javadoc以找到从输入流构建JDOM2文档的方法,那应该不难。

Java has a ton of libraries for working with XML. Java有大量用于处理XML的库。 In addition to the many classes that work with XML included with the standard Java installation, there are lots of other open source libraries available. 除了标准Java安装中包含的许多使用XML的类之外,还有许多其他开源库可用。 Here are a few options. 这里有一些选择。 Take a look at the docs and see which one meets your needs: 查看文档,看看哪个符合您的需求:

The XStream Library is very fast and easy to use. XStream库非常快速且易于使用。 I've used it for XML serialization and I'm very happy with it. 我已将它用于XML序列化,我对此非常满意。 If you would rather not use an extrenal library, then try the javax.xml.parsers.DocumentBuilder class that is demonstrated here 如果您不想使用extrenal库,请尝试此处演示的javax.xml.parsers.DocumentBuilder

If you want to parse a String str to Document in Java, you can do following: 如果要将String str解析为Java中的Document ,可以执行以下操作:

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
        InputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
        return docBuilder.parse(is);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM