如何将子元素从 XML 提取到 Java 中的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/632043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I extract child element from XML to a string in Java?
提问by phil swenson
If I have an XML document like
如果我有一个 XML 文档,例如
<root>
<element1>
<child attr1="blah">
<child2>blahblah</child2>
<child>
</element1>
</root>
I want to get an XML string with the first child element. My output string would be
我想获得一个带有第一个子元素的 XML 字符串。我的输出字符串将是
<element1>
<child attr1="blah">
<child2>blahblah</child2>
<child>
</element1>
There are many approaches, would like to see some ideas. I've been trying to use Java XML APIs for it, but it's not clear that there is a good way to do this.
方法有很多,想看一些思路。我一直在尝试使用 Java XML API,但不清楚是否有一种好方法可以做到这一点。
thanks
谢谢
采纳答案by Matt McMinn
You're right, with the standard XML API, there's not a good way - here's one example (may be bug ridden; it runs, but I wrote it a long time ago).
您是对的,对于标准的 XML API,没有一个好方法——这里有一个例子(可能是 bug 缠身;它可以运行,但我很久以前写的)。
import javax.xml.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
import java.io.*;
public class Proc
{
public static void main(String[] args) throws Exception
{
//Parse the input document
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File("in.xml"));
//Set up the transformer to write the output string
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
//Find the first child node - this could be done with xpath as well
NodeList nl = doc.getDocumentElement().getChildNodes();
DOMSource source = null;
for(int x = 0;x < nl.getLength();x++)
{
Node e = nl.item(x);
if(e instanceof Element)
{
source = new DOMSource(e);
break;
}
}
//Do the transformation and output
transformer.transform(source, result);
System.out.println(sw.toString());
}
}
It would seem like you could get the first child just by using doc.getDocumentElement().getFirstChild(), but the problem with that is if there is any whitespace between the root and the child element, that will create a Text node in the tree, and you'll get that node instead of the actual element node. The output from this program is:
看起来你可以通过使用 doc.getDocumentElement().getFirstChild() 来获得第一个孩子,但问题是如果根元素和子元素之间有任何空格,这将在树,您将获得该节点而不是实际的元素节点。这个程序的输出是:
D:\home\tmp\xml>java Proc
<?xml version="1.0" encoding="UTF-8"?>
<element1>
<child attr1="blah">
<child2>blahblah</child2>
</child>
</element1>
I think you can suppress the xml version string if you don't need it, but I'm not sure on that. I would probably try to use a third party XML library if at all possible.
我认为如果您不需要它,您可以取消 xml 版本字符串,但我不确定。如果可能的话,我可能会尝试使用第三方 XML 库。
回答by duffymo
回答by Mark
If your xml has schema backing it, you could use xmlbeans or JAXB to generate pojo objects that help you marshal/unmarshal xml.
如果您的 xml 有支持它的模式,您可以使用 xmlbeans 或 JAXB 来生成 pojo 对象来帮助您编组/解组 xml。
回答by TofuBeer
XMLBeansis an easy to use (once you get the hang of it) tool to deal with XML without having to deal with the annoyances of parsing.
XMLBeans是一个易于使用(一旦您掌握了它的窍门)工具来处理 XML,而无需处理解析的烦恼。
It requires that you have a schema for the XML file, but it also provides a tool to generate a schema from an exisint XML file (depending on your needs the generated on is probably fine).
它要求您拥有 XML 文件的架构,但它还提供了从现有 XML 文件生成架构的工具(根据您的需要,生成的架构可能没问题)。
回答by Monica
Since this is the top google answer and For those of you who just want the basic:
由于这是最重要的谷歌答案,对于那些只想要基本答案的人:
public static String serializeXml(Element element) throws Exception
{
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
StreamResult result = new StreamResult(buffer);
DOMSource source = new DOMSource(element);
TransformerFactory.newInstance().newTransformer().transform(source, result);
return new String(buffer.toByteArray());
}
I use this for debug, which most likely is what you need this for
我用它来调试,这很可能是你需要的
回答by yurin
As question is actually about first occurrence of string inside another string, I would use String class methods, instead of XML parsers:
由于问题实际上是关于在另一个字符串中第一次出现字符串,我将使用 String 类方法,而不是 XML 解析器:
public static String getElementAsString(String xml, String tagName){
int beginIndex = xml.indexOf("<" + tagName);
int endIndex = xml.indexOf("</" + tagName, beginIndex) + tagName.length() + 3;
return xml.substring(beginIndex, endIndex);
}
回答by Preksha Harne
public String getXML(String xmlContent, String tagName){
String startTag = "<"+ tagName + ">";
String endTag = "</"+ tagName + ">";
int startposition = xmlContent.indexOf(startTag);
int endposition = xmlContent.indexOf(endTag, startposition);
if (startposition == -1){
return "ddd";
}
startposition += startTag.length();
if(endposition == -1){
return "eee";
}
return xmlContent.substring(startposition, endposition);
}
Pass your xml as string to this method,and in your case pass 'element' as parameter tagname.
将您的 xml 作为字符串传递给此方法,在您的情况下,将“元素”作为参数标记名传递。
回答by Hemant Thorat
You can use following function to extract xml block as string by passing proper xpath expression,
您可以使用以下函数通过传递正确的 xpath 表达式将 xml 块提取为字符串,
private static String nodeToString(Node node) throws TransformerException
{
StringWriter buf = new StringWriter();
Transformer xform = TransformerFactory.newInstance().newTransformer();
xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
xform.transform(new DOMSource(node), new StreamResult(buf));
return(buf.toString());
}
public static void main(String[] args) throws Exception
{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
XPath xPath = XPathFactory.newInstance().newXPath();
Node result = (Node)xPath.evaluate("A/B/C", doc, XPathConstants.NODE); //"A/B[id = '1']" //"//*[@type='t1']"
System.out.println(nodeToString(result));
}