Java XML Parse问题

时间:2017-02-12 01:01:23

标签: java xml

我在访问XML值时遇到困难,任何人都可以让我知道我错过了什么。

t = 3600:50:172800;x = 0.1;y = 0; ro = 0.1;

T = zeros(1,length(t));

for Cm = 1E6:1E6:4E6
    for i = 1:length(t)
        T = T_ILS(x,y,ro,Cm,t);
        Tall(Cm,:) = [T];
    end
end

2 个答案:

答案 0 :(得分:2)

问题是,您是否没有名为" Standard"和" ChangeState",您的节点具有Standard和ChangeState的命名空间版本。

因此,您需要使用XPath.setNamespace()方法将前缀与命名空间相关联,然后在xpath表达式中使用该前缀。请注意,因为您只在查询中使用默认命名空间,所以您必须指定,例如

   String xml = "<Standard p1:oid=\"00000000-0000-0000-0000-000000f674c1\"\n"
            + "                               xmlns:p1=\"com.iMelt.metaCore.DataObjects.Core\" xmlns=\"com.iMelt.Car.Model.Core\"\n"
            + "                               reasonCode=\"0\">\n" + "    <p1:__info p1:eid=\"00000000-0000-0000-0000-000000000000\">\n"
            + "        <p1:creationDate>2016-05-28T20:33:45.337+00:00</p1:creationDate>\n" + "        <p1:lastEditorRef>Administrator</p1:lastEditorRef>\n"
            + "        <p1:version>5</p1:version>\n" + "    </p1:__info>\n" + "    <ChangeState>Approved</ChangeState></Standard>";

    byte[] byteArray = xml.getBytes();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(inputStream);
    XPathFactory xpathfactory = XPathFactory.newInstance();
    XPath xpath = xpathfactory.newXPath();
    xpath.setNamespaceContext(new NamespaceContext() {

        @Override
        public String getNamespaceURI(String prefix) {
            return "com.iMelt.Car.Model.Core";
        }

        @Override
        public String getPrefix(String namespaceURI) {

            return "x";
        }

        @Override
        public Iterator getPrefixes(String namespaceURI) {
            return null;
        }

    });
    XPathExpression expr = xpath.compile("/x:Standard/x:ChangeState");
    NodeList result = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

另一个效果较差的选项是删除语句

factory.setNamespaceAware(true);

答案 1 :(得分:0)

以下代码输出“已批准”。我已删除factory.setNamespaceAware(true);,因此您无需指定默认命名空间并在末尾添加代码以输出文本值。

import java.util.*;
import java.lang.*;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.xpath.*;

// ...

String xml = "<Standard p1:oid=\"00000000-0000-0000-0000-000000f674c1\"\n" +
                "                               xmlns:p1=\"com.iMelt.metaCore.DataObjects.Core\" xmlns=\"com.iMelt.Car.Model.Core\"\n" +
                "                               reasonCode=\"0\">\n" +
                "    <p1:__info p1:eid=\"00000000-0000-0000-0000-000000000000\">\n" +
                "        <p1:creationDate>2016-05-28T20:33:45.337+00:00</p1:creationDate>\n" +
                "        <p1:lastEditorRef>Administrator</p1:lastEditorRef>\n" +
                "        <p1:version>5</p1:version>\n" +
                "    </p1:__info>\n" +
                "    <ChangeState>Approved</ChangeState></Standard>";

byte[] byteArray = xml.getBytes();
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(inputStream);
XPathFactory xpathfactory = XPathFactory.newInstance();
XPath xpath = xpathfactory.newXPath();
XPathExpression expr = xpath.compile("/Standard/ChangeState");
NodeList result = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);
Node node = result.item(0);
System.out.println(node.getTextContent());