各位,我写了一个把包含XML信息的String转化成Docuemnt对象的方法,为什么总是不对啊?
package com.unittest;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;

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

import junit.framework.TestCase;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class StringXMLTest extends TestCase {
    public void testConsole(){
        String xml1 = "<CODEITEM><ID>10004</ID><NAME>4</NAME><CONTENT>it is the 4th item</CONTENT></CODEITEM>";
        String xml2 = "<CODEITEM><ID>10004</ID><NAME>4</NAME><CONTENT>这是第4个item。</CONTENT></CODEITEM>";
        String xml3 = "<CODEITEM><ID><![CDATA[10004]]></ID><NAME><![CDATA[4]]></NAME><CONTENT><![CDATA[这是第4个item。]]></CONTENT></CODEITEM>";
        String xml4 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><CODEITEM><ID><![CDATA[10004]]></ID><NAME><![CDATA[4]]></NAME><CONTENT><![CDATA[这是第4个item。]]></CONTENT></CODEITEM>";
        String[] xml = new String[]{xml1,xml2,xml3,xml4};
        Document doc;
        Element root;
        String id,name,content;
        try {
            for (int i = 0; i < xml.length; i++) {
                System.out.println("xml" + (i+1) + ":");
                System.out.println("method1:");
                doc = transferStringToDocument1(xml[i]);
                root = (Element) doc.getElementsByTagName("CODEITEM").item(0);
                
                id = root.getAttribute("ID");
                name = root.getAttribute("NAME");
                content = root.getAttribute("CONTENT");
                System.out.println("id = " + id);
                System.out.println("name = " + name);
                System.out.println("content = " + content);
                
                System.out.println("method2:");
                doc = transferStringToDocument2(xml[i]);
                root = (Element) doc.getElementsByTagName("CODEITEM").item(0);
                
                id = root.getAttribute("ID");
                name = root.getAttribute("NAME");
                content = root.getAttribute("CONTENT");
                System.out.println("id = " + id);
                System.out.println("name = " + name);
                System.out.println("content = " + content + "\n");
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private Document transferStringToDocument1(String s) throws ParserConfigurationException, SAXException, IOException{
        StringReader reader = new StringReader(s);
        InputSource is = new InputSource(reader);
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(is);
        
        return doc;
    }
    
    private Document transferStringToDocument2(String s) throws ParserConfigurationException, SAXException, IOException{
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(s.getBytes()));
        
        return doc;
    }
    
    
}

 

2个回答

获取结果的方法错了应该是
[code="java"]
id = root.getFirstChild().getTextContent();

name = root.getChildNodes().item(1).getTextContent();

content = root.getChildNodes().item(2).getTextContent();
[/code]

transferStringToDocument2 这个方法和当明编码格式有关最好不要用

[code="java"]
root = (Element) doc.getFirstChild();

Element idElement = (Element) root.getElementsByTagName("ID").item(0);
Element nameElement = (Element) root.getElementsByTagName("NAME").item(0);
Element contentElement=(Element) root.getElementsByTagName("CONTENT").item(0);

id = idElement.getTextContent();
name = nameElement.getTextContent();
content = contentElement.getTextContent();

[/code]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐