一个项目里面需要xml解析,有4种结构,其中3个比较复杂的都ok了,反倒是一个简单的无论如何都通不过,请高手帮我看看
xml文档结构:<?xml version="1.0" encoding="gb18030"?>
<FDBK>
<SFID>RQCR</SFID>
<RCTN>2</RCTN>
<FCRCs>
<FCRC seqno="1">
<RCLC>1111111111</RCLC>
<RCSG>test文本111111</RCSG>
</FCRC>
<FCRC seqno="2">
<RCLC>22222222222</RCLC>
<RCSG>test文本222222222222</RCSG>
</FCRC>
</FCRCs>
</FDBK>解析的代码我提出来了,把原来入库部分变成了System.out.println方便调试,代码如下:import java.io.File;
import java.io.IOException;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class xmltest { /**
 * @param args
 * @throws ParserConfigurationException 
 */

public static void main(String[] args) throws ParserConfigurationException {
// TODO Auto-generated method stub

File file = new File("D:\\FDRC[NBS011021101020001-20081214-0001-0001]20081214124530.XML");

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
Element root = doc.getDocumentElement();
NodeList children = root.getChildNodes();
System.out.println(children.getLength());
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeName().equals("SFID")) {
System.out.println(child.getTextContent());
} else if (child.getNodeName().equals("RCTN")) {
System.out.println(Integer.valueOf(child.getTextContent()));
} else if (child.getNodeName().equals("FCRCs")) { NodeList fcrcs = child.getChildNodes();
System.out.println("显示fcrc子节点");
System.out.println(fcrcs.getLength());
for (int j = 0; j < fcrcs.getLength(); j++) {
Node fcrcNode = fcrcs.item(j);
System.out.println(fcrcNode);
if(fcrcNode.getNodeName().equals("FCRC")){
System.out.println(fcrcNode.getAttributes().item(0).getTextContent());
System.out.println(fcrcNode.getChildNodes().getLength());
for(int k=0;k<fcrcNode.getChildNodes().getLength();k++){
if(fcrcNode.getChildNodes().item(k).getNodeName().equals("RCLC"))
System.out.println(fcrcNode.getChildNodes().item(k).getNodeValue());
if(fcrcNode.getChildNodes().item(k).getNodeName().equals("RCSG"))
System.out.println(fcrcNode.getChildNodes().item(k).getNodeValue());
}
} }
}
}

} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

}
}}解析到获得fcrc节点以后,读出的fcrc是空的,不知道问什么,在线等候中,先多谢了

解决方案 »

  1.   

    现在跑完的输出是
    7
    RQCR
    2
    显示fcrc子节点
    5
    [#text: 
    ]
    [FCRC: null]
    1
    5
    null
    null
    [#text: 
    ]
    [FCRC: null]
    2
    5
    null
    null
    [#text: 
    ]
    太奇怪了
      

  2.   

                                    if(fcrcNode.getChildNodes().item(k).getNodeName().equals("RCLC"))
                                        System.out.println(fcrcNode.getChildNodes().item(k).getNodeValue());
                                    if(fcrcNode.getChildNodes().item(k).getNodeName().equals("RCSG"))
                                        System.out.println(fcrcNode.getChildNodes().item(k).getNodeValue());
    这里写错了                                if(fcrcNode.getChildNodes().item(k).getNodeName().equals("RCLC"))
                                        System.out.println(fcrcNode.getChildNodes().item(k).getTextContent());
                                    if(fcrcNode.getChildNodes().item(k).getNodeName().equals("RCSG"))
                                        System.out.println(fcrcNode.getChildNodes().item(k).getTextContent());另外,建议楼主用dom4j来做XML解析,比java xml api好用得多!
      

  3.   

    dom4j的代码例子:public class Dom4jImpl { @Override
    public long readXmlToList(List<Student> students, String fileName) {
    long begin = System.currentTimeMillis();
    SAXReader reader = new SAXReader(); 
    Document doc =null;
    try {
    doc = reader.read(fileName);
    } catch (DocumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

    Element root = doc.getRootElement();
    if (students == null)
    students = new ArrayList<Student>();
    Iterator<?> i = root.elementIterator("student");
    while(i.hasNext()){
    Element e=(Element)i.next(); Student stu = new Student();
    stu.setId(Integer.parseInt(e.attributeValue("id")));
    stu.setName(e.elementText("name"));
    stu.setAddress(e.elementText("address"));
    stu.setAge(Integer.parseInt(e.elementText("age")));
    //System.out.println(stu);
    } return System.currentTimeMillis() - begin;
    } @Override
    public long writeListToXml(List<Student> students, String fileName) {
    long begin = System.currentTimeMillis();
    Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("students");// �Ƚ�b��Ԫ��"ѧ�����"

    for (Student stu : students) {
    Element student = root.addElement("student");// ��b��ѧ��Ԫ�أ���ӵ���Ԫ��
    student.addAttribute("id", String.valueOf(stu.getId())); student.addElement("name").setText(stu.getName());
    student.addElement("address").setText(stu.getAddress());
    student.addElement("age").setText(String.valueOf(stu.getAge())); }
    OutputStreamWriter os=null;
    try {
    os = new OutputStreamWriter(new FileOutputStream(fileName),"UTF-8");
    XMLWriter writer = new XMLWriter(os,new OutputFormat("\t",true));
            writer.write(doc);
            writer.close();
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }  return System.currentTimeMillis() - begin;
    }}
      

  4.   

    对于Element元素,getNodeName()会返回Element.tagName,getNodeValue()返回null所有元素调用getNodeName()和getNodeValue()的值如下:
    Interface     nodeName     nodeValue     attributes 
    Attr 与 Attr.name 相同 与 Attr.value 相同 null 
    CDATASection "#cdata-section" 与 CharacterData.data 相同,CDATA 节的内容 null 
    Comment "#comment" 与 CharacterData.data 相同,该注释的内容 null 
    Document "#document" null null 
    DocumentFragment "#document-fragment" null null 
    DocumentType 与 DocumentType.name 相同 null null 
    Element 与 Element.tagName 相同 null NamedNodeMap 
    Entity entity name null null 
    EntityReference 引用的实体名称 null null 
    Notation notation name null null 
    ProcessingInstruction 与 ProcessingInstruction.target 相同 与 ProcessingInstruction.data 相同 null 
    Text "#text" 与 CharacterData.data 相同,该文本节点的内容 null 
      

  5.   

    问题已经解决,谢谢各位的帮助。我换了写法,去掉从FCRCs获取子节点的办法,直接用root.getElementsByTag来获取FCRC节点,然后下面获取内容的方法换成getTextContent());,并且调整了item()中的号码,变成1 和3 ,就能正常获得了,总结一下,应该是getNodeValue()这个方法的问题,我后来又试了几种xml结构,用这个方法都无法准确获得。另外,这个项目是二期工程,一期不是我做的,原来用得是这种解析方式,为了保持一致,不得已用了这种方法,比较郁闷。不过,解决了就是件好事,再次多谢各位的帮助