如何从Java中将子元素从XML提取到字符串? [英] How do I extract child element from XML to a string in Java?

查看:105
本文介绍了如何从Java中将子元素从XML提取到字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有XML文档,例如

If I have an XML document like

<root>   
   <element1>
        <child attr1="blah">
           <child2>blahblah</child2>
        <child>   
   </element1> 
</root>

我想获得带有第一个子元素的XML字符串。我的输出字符串是

I want to get an XML string with the first child element. My output string would be

<element1>
    <child attr1="blah">
       <child2>blahblah</child2>
    <child>
</element1>

有很多方法,希望看到一些想法。我一直在尝试使用Java XML API,但目前还不清楚是否有一种很好的方法可以做到这一点。

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.

谢谢

推荐答案

你是对的,使用标准的XML API,并不是一个好方法 - 这是一个例子(可能是错误的;它运行,但我写了很久以前)。

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).

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());
    }
}

看起来你可以得到第一个孩子通过使用doc.getDocumentElement()。getFirstChild(),但问题是如果root和child元素之间有任何空格,那么将在树中创建一个Text节点,你将获得该节点而不是实际的元素节点。该程序的输出是:

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:

D:\home\tmp\xml>java Proc
<?xml version="1.0" encoding="UTF-8"?>
<element1>
        <child attr1="blah">
           <child2>blahblah</child2>
       </child>
   </element1>

我认为你可以抑制xml版本字符串,如果你不需要它,但我是不确定。如果可能的话,我可能会尝试使用第三方XML库。

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.

这篇关于如何从Java中将子元素从XML提取到字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆