Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XML解析文档。解析,名称错误除外_Xml_Blackberry_Java Me - Fatal编程技术网

XML解析文档。解析,名称错误除外

XML解析文档。解析,名称错误除外,xml,blackberry,java-me,Xml,Blackberry,Java Me,Hy, 我的xml解析器再次出现问题:D 我用org.w3c.dom.Document解析字符串 他说: 但我收到一条错误消息:需要一个名字 字符串已重新格式化为可读: <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/20

Hy, 我的xml解析器再次出现问题:D

我用org.w3c.dom.Document解析字符串 他说:

但我收到一条错误消息:需要一个名字

字符串已重新格式化为可读:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <soap:Fault>
      <faultcode>soap:Server</faultcode>
      <faultstring>Server was unable to process request. ---&gt; Invalid Username or password</faultstring>
      <detail />
    </soap:Fault>
  </soap:Body>
</soap:Envelope>
试试这个

DocumentBuilderFactory factory1 = DocumentBuilderFactory.newInstance();

DocumentBuilder builder1 = factory1.newDocumentBuilder();

XMLDOMUtil xm1 = new XMLDOMUtil();

ByteArrayInputStream bis = new ByteArrayInputStream(res.getBytes("UTF-8"));

Document document = builder1.parse(bis);

Node root = xm1.getNodeByTag(document, "soap:Envelope");

Node nextroot= xm1.getNodeByTag(root,"soap:Body");

Node nextroot1= xm1.getNodeByTag(nextroot,"soap:Fault");

String faultstring= xm1.getNodeTextByTag(nextroot1,"faultstring");
下面给出了XMLDOMUtil类-

import org.w3c.dom.Node;
import org.w3c.dom.Text;

public class XMLDOMUtil {



// go thru the list of childs and find the text associated by the tag
public String getNodeTextByTag(Node parentNode, String name) {

    Node node = parentNode.getFirstChild();
    Text text = null;
    String retStr = null;

    try {
        while (node != null) {
            if (node.getNodeName().equals(name)) {
                text = (Text) node.getFirstChild();
                retStr = text.getData();
                break;
            }
            node = node.getNextSibling();
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return retStr;
}

public Node getNodeByTag(Node parentNode, String name) {

    Node node = parentNode.getFirstChild();
    Node retNode = null;

    while (node != null) {
        if (node.getNodeName().equals(name)) {
            retNode = node;
            break;
        }
        node = node.getNextSibling();
    }
    return retNode;
}

public Node getNextSiblingNodeByTag(Node siblingNode, String name) {

    Node retNode = null;

    siblingNode = siblingNode.getNextSibling();

    while (siblingNode != null) {
        if (siblingNode.getNodeName().equals(name)) {
            retNode = siblingNode;
            break;
        }
        siblingNode = siblingNode.getNextSibling();
    }
    return retNode;
}

}
import org.w3c.dom.Node;
import org.w3c.dom.Text;

public class XMLDOMUtil {



// go thru the list of childs and find the text associated by the tag
public String getNodeTextByTag(Node parentNode, String name) {

    Node node = parentNode.getFirstChild();
    Text text = null;
    String retStr = null;

    try {
        while (node != null) {
            if (node.getNodeName().equals(name)) {
                text = (Text) node.getFirstChild();
                retStr = text.getData();
                break;
            }
            node = node.getNextSibling();
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return retStr;
}

public Node getNodeByTag(Node parentNode, String name) {

    Node node = parentNode.getFirstChild();
    Node retNode = null;

    while (node != null) {
        if (node.getNodeName().equals(name)) {
            retNode = node;
            break;
        }
        node = node.getNextSibling();
    }
    return retNode;
}

public Node getNextSiblingNodeByTag(Node siblingNode, String name) {

    Node retNode = null;

    siblingNode = siblingNode.getNextSibling();

    while (siblingNode != null) {
        if (siblingNode.getNodeName().equals(name)) {
            retNode = siblingNode;
            break;
        }
        siblingNode = siblingNode.getNextSibling();
    }
    return retNode;
}

}