2012-07-11 46 views
0

我在Java编程(最终在Android)和我有一组像这样的Java/Android的:XML解析让所有的XML在某个标签

<A> 
    <B> 
    <C>stuff</C> 
    <D> 
     <E>other stuff</E> 
     <F>more stuff</F> 
    </D> 
    </B> 

    <B> 
    <C>stuff</C> 
    </B> 

    <B> 
    <C>some stuff</C> 
    <D> 
     <E>basic stuff</E> 
     <F>even more stuff</F> 
    </D> 
    </B> 
</A> 

我想分析它使我们得到(除我已经编码的其他东西)都在这两个D的东西,所以我们会得到的字符串,看起来像

<E>other stuff</E> 
<F>more stuff</F> 

一个空字符串(“”)和

<E>basic stuff</E> 
<F>even more stuff</F> 

我一直在使用的解析器只要碰到小于'<'的符号就停止,所以它一直没有给我。有没有办法像我在Java中描述的那样解析它?

编辑:我只是将它转换为字符串并使用正则表达式。

+0

任何体面的解析器应该能够为你做到这一点,没有多少努力,酒吧可能在中间得到空字符串,你的代码看起来如何? – 2012-07-11 16:33:15

+0

TheList = firstClientElement.getElementsByTagName(“D”); TheElement =(Element)TheList.item(0); \t \t \t \t \t 如果(TheList.item(0)!= NULL){ 的thelist = TheElement.getChildNodes(); output = TheList.item(0).getNodeValue(); } 我找不到任何如何取回标签中的XML作为字符串的例子。 – user1515993 2012-07-11 16:37:34

回答

0

要将解析的XML转换回字符串,可以使用javax.xml.transform.Transformer。我已经附加代码解析您的示例XML和打印所有D元素到控制台 - 我想你就可以把它变成你想要的东西:)

// The below is simply to create a document to test the code with 
String xml = "<A><B><C>stuff</C><D><E>other stuff</E><F>more stuff</F></D></B><B><C>stuff</C></B><B><C>some stuff</C><D><E>basic stuff</E><F>even more stuff</F></D></B></A>"; 

DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
InputSource docSource = new InputSource(new StringReader(xml)); 
Document document = documentBuilder.parse(docSource); 
// The above is simply to create a document to test the code with 

// Transformer takes a DOMSource pointed at a Node and outputs it as text 
Transformer transformer = TransformerFactory.newInstance().newTransformer(); 
// Add new lines for every element 
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
// Skip the <? xml ... ?> prolog 
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 

NodeList elements = document.getElementsByTagName("D"); 
StringWriter sw = new StringWriter(); 
StreamResult res = new StreamResult(sw); 
DOMSource source = new DOMSource(); 
for (int i = 0; i < elements.getLength(); i++) { 
    Element element = (Element) elements.item(i); 
    source.setNode(element); 
    // Write the current element to the stringwriter via the streamresult 
    transformer.transform(source, res); 
} 
System.out.println(sw.toString()); 

如果你只想要的内容元素,你可以像这样替换for循环:

for (int i = 0; i < elements.getLength(); i++) { 
    Element element = (Element) elements.item(i); 
    NodeList childNodes = element.getChildNodes(); 
    for (int j = 0; j < childNodes.getLength(); j++) { 
     Node childNode = childNodes.item(j); 
     source.setNode(childNode); 
     transformer.transform(source, res); 
    } 

} 
+0

谢谢,这有帮助,现在我只想弄清楚一些代码的工作原理。我最后并不需要D,但如果我不知道如何删除它们,我可以将它们解析出来。 – user1515993 2012-07-11 17:08:21

+0

其实我可能需要别的东西,因为你可以有 user1515993 2012-07-11 17:30:21

+0

见我的编辑,我想。该代码,是会给你' \ n \ n',修改会给你''。我在代码中添加了一些注释以帮助您开始。 – 2012-07-12 07:57:06

0

你需要使用一个已经写好的解析器。

不要使用自己滚动的游戏,而只是要求自己解决问题。

+0

它不是一个正则表达式的解析器,它只是获取之间的所有内容 – user1515993 2012-08-08 22:45:00