2013-03-26 122 views
0

我是Java新手,需要一些帮助。 我有一个XML看起来像:Java string to xml to list

String pXML = 
"<root> 
    <x>1</x> 
    <x>2</x> 
    <x>3</x> 
    <x>4</x> 
</root>" 

而且我想获得一个包含所有X标签内的值的列表对象。

我试着javax.xml.parsers.DocumentBuilderFactory中:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = factory.newDocumentBuilder(); 
document = (Document) builder.parse(new InputSource(new StringReader(pXML))); 
Node n = document.getFirstChild(); 
NodeList n1 = n.getChildNodes(); 
//and then I go through all the nodes and insert the values into a list 

但是,这并不包含x个节点。

+1

我怀疑引号是导致解析器跳过所有的XML。 – 2013-03-26 13:47:33

+0

不,我得到的pXML对象作为输入参数,这是为了了解xml的外观...... – informerica 2013-03-26 13:57:11

回答

3

您可以使用XPath来获取所有x节点的值如下:

public static void main(String[] args) throws SAXException, ParserConfigurationException, IOException, XPathExpressionException { 
    final String pXML = "<root><x>1</x><x>2</x><x>3</x><x>4</x></root>"; 
    final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(pXML.getBytes())); 
    final XPathExpression xPathExpression = XPathFactory.newInstance().newXPath().compile("//x/text()"); 
    final NodeList nodeList = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET); 
    final List<String> values = new LinkedList<>(); 
    for (int i = 0; i < nodeList.getLength(); ++i) { 
     values.add(nodeList.item(i).getNodeValue()); 
    } 
    System.out.println(values); 
} 

输出:

[1, 2, 3, 4] 

这具有作为非常通用的解决方案的优点,如果XML的结构发生变化,则可以很容易地适应这种情况。

在我看来,它的优点是比用手在Document上迭代节点更容易理解。

+0

同意并使用xPath不太容易出错。迭代一个元素的子节点可能会给你一个混合文本节点和(子)元素,你不一定期望。 – 2013-03-26 14:15:23

+0

谢谢你这个魅力! – informerica 2013-03-26 14:30:07

+0

找出问题所在 - 在我的代码中,NodeList中的NodeValues具有未修剪的值,因此它具有来自'\ n'和''的大量值。只是乱七八糟的字符和字符串... – informerica 2013-03-26 14:32:32

0

尝试Node n = document.getDocumentElement();恢复您的XML的根元素

0

尝试这个

import java.io.StringReader; 
import java.util.ArrayList; 
import java.util.List; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 

import org.w3c.dom.CharacterData; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 

public class XML { 
    public static void main(String arg[]) throws Exception{ 
    String xmlRecords = "<root><x>1</x><x>2</x><x>3</x><x>4</x></root>"; 

    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
    InputSource is = new InputSource(); 
    is.setCharacterStream(new StringReader(xmlRecords)); 

    Document doc = db.parse(is); 
    NodeList nodes = doc.getElementsByTagName("x"); 
    System.out.println(nodes.getLength()); 
    List<String> valueList = new ArrayList<String>(); 
    for (int i = 0; i < nodes.getLength(); i++) { 
     Element element = (Element) nodes.item(i); 

     String name = element.getTextContent(); 

    // Element line = (Element) name.item(0); 
     System.out.println("Name: " + name); 
     valueList.add(name); 
    } 

    } 
}