2013-03-26 110 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); 
    } 

    } 
}