Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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
Java h表达式是一个编译时常量-虽然它可能与任何内容都不匹配,但至少您知道它不会引发异常。@Sembrano为了使用XPath,您必须使用名称空间感知解析器解析文档-调用builderFactory.setNamespaceAwaretrue;在执行builde_Java_Xml_Xpath - Fatal编程技术网

Java h表达式是一个编译时常量-虽然它可能与任何内容都不匹配,但至少您知道它不会引发异常。@Sembrano为了使用XPath,您必须使用名称空间感知解析器解析文档-调用builderFactory.setNamespaceAwaretrue;在执行builde

Java h表达式是一个编译时常量-虽然它可能与任何内容都不匹配,但至少您知道它不会引发异常。@Sembrano为了使用XPath,您必须使用名称空间感知解析器解析文档-调用builderFactory.setNamespaceAwaretrue;在执行builde,java,xml,xpath,Java,Xml,Xpath,h表达式是一个编译时常量-虽然它可能与任何内容都不匹配,但至少您知道它不会引发异常。@Sembrano为了使用XPath,您必须使用名称空间感知解析器解析文档-调用builderFactory.setNamespaceAwaretrue;在执行builderFactory.newDocumentBuilder之前。除此之外,它应该会起作用,假设您在问题中给出的XML是真实结构的真实表示。@Sembrano您发布的代码不调用setNamespaceAwaretrue@Sembrano这就是我所说的


h表达式是一个编译时常量-虽然它可能与任何内容都不匹配,但至少您知道它不会引发异常。@Sembrano为了使用XPath,您必须使用名称空间感知解析器解析文档-调用builderFactory.setNamespaceAwaretrue;在执行builderFactory.newDocumentBuilder之前。除此之外,它应该会起作用,假设您在问题中给出的XML是真实结构的真实表示。@Sembrano您发布的代码不调用setNamespaceAwaretrue@Sembrano这就是我所说的假设您在问题中给出的XML是真实结构的真实表示——如果不是,那么您显然必须调整XPath表达式与此相匹配,web上有许多XPath教程可以帮助您针对具体情况计算出正确的表达式。@Sembrano您可以为vars.puttype。。。呼叫我总是建议您不要通过将用户提供的字符串连接在一起来构建XPath表达式,这是一种诱人的XPath,相当于SQL注入攻击。@NickHolt攻击可能有点强,但本质上,如果允许用户提供的输入成为表达式的一部分,则无法保证它甚至可以编译—XPath的经典示例是无法在一个字符串中同时包含单引号和双引号文字双引号字符串可以包含单引号,反之亦然,但没有转义机制允许一个字符串同时包含两种类型。使用变量解析器时,实际的XPath表达式是一个编译时常量,虽然它可能与任何内容都不匹配,但至少您知道它不会引发异常。@Sembrano为了使用XPath,您必须使用名称空间感知解析器解析您的文档-调用builderFactory.setNamespaceAwaretrue;在执行builderFactory.newDocumentBuilder之前。除此之外,它应该会起作用,假设您在问题中给出的XML是真实结构的真实表示。@Sembrano您发布的代码不调用setNamespaceAwaretrue@Sembrano这就是我所说的假设您在问题中给出的XML是真实结构的真实表示——如果不是,那么您显然必须调整XPath表达式与此相匹配,web上有许多XPath教程,可以帮助您针对特定情况计算出正确的表达式。
String expression = "/getObjType()/prop/*"; 
<type>
  <OBJECT_TYPE>SiteData</OBJECT_TYPE> 
  <prop>
    <DESCRIPTION>Site parameters</DESCRIPTION> 
    <PARENT>NULL</PARENT> 
    <VIRTUAL>0</VIRTUAL> 
    <VISIBLE>1</VISIBLE> 
    <PICTURE>NULL</PICTURE> 
    <HELP>10008</HELP> 
    <MIN_NO>1</MIN_NO> 
    <MAX_NO>1</MAX_NO> 
    <NAME_FORMAT>NULL</NAME_FORMAT> 
  </prop>
  <param>
    <PARAMETER>blabla</PARAMETER> 
    <DATA_TYPE>INTEGER</DATA_TYPE> 
    <DESCRIPTION>blaba</DESCRIPTION> 
    <MIN_NO>1</MIN_NO> 
    <MAX_NO>1</MAX_NO> 
    <ORDER1>1</ORDER1> 
    <NESTED>0</NESTED> 
    <DEFAULT1>NULL</DEFAULT1> 
    <FORMAT>0:16382</FORMAT> 
  </param>
  <OBJECT_TYPE>Data</OBJECT_TYPE> 
  <prop>
    <DESCRIPTION>Site parameters</DESCRIPTION> 
    <PARENT>NULL</PARENT> 
    <VIRTUAL>0</VIRTUAL> 
    <VISIBLE>1</VISIBLE> 
    <PICTURE>NULL</PICTURE> 
    <HELP>10008</HELP> 
    <MIN_NO>1</MIN_NO> 
    <MAX_NO>1</MAX_NO> 
    <NAME_FORMAT>NULL</NAME_FORMAT> 
  </prop>
  <param>
    <PARAMETER>gmgm</PARAMETER> 
    <DATA_TYPE>INTEGER</DATA_TYPE> 
    <DESCRIPTION>babla</DESCRIPTION> 
    <MIN_NO>1</MIN_NO> 
    <MAX_NO>1</MAX_NO> 
    <ORDER1>1</ORDER1> 
    <NESTED>0</NESTED> 
    <DEFAULT1>NULL</DEFAULT1> 
    <FORMAT>0:16382</FORMAT> 
  </param>
</type>
 public class PropXMLParsing {

    static PropXMLParsing instance = null;

    private List<String> list = new ArrayList<String>();
    ObjType obj = new ObjType();

    public static PropXMLParsing getInstance() {

        if (instance == null) {

            instance = new PropXMLParsing();
            try {
                instance.ParserForObjectTypes();
            } catch (SAXException e) {

                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            } catch (ParserConfigurationException e) {

                e.printStackTrace();
            }

        }

        return instance;

    }

    public void ParserForObjectTypes() throws SAXException, IOException,
            ParserConfigurationException {

        try {
            FileInputStream file = new FileInputStream(new File(
                    "xmlFiles/CoreDatamodel.xml"));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory
                    .newInstance();

            builderFactory.setNamespaceAware(true);
            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            XPath xp = XPathFactory.newInstance().newXPath();
            final Map<String, Object> vars = new HashMap<String, Object>();
            xp.setXPathVariableResolver(new XPathVariableResolver() {
                public Object resolveVariable(QName name) {
                    return vars.get(name.getLocalPart());
                }
            });

            XPathExpression expr = xp
                    .compile("/type/OBJECT_TYPE[. = $type]/following-sibling::prop[1]");

            vars.put("type", obj.getObjectType());
            NodeList objectProps = (NodeList) expr.evaluate(xmlDocument,
                    XPathConstants.NODESET);
            System.out.println(objectProps);

            for (int i = 0; i < objectProps.getLength(); i++) {

                System.out.println(objectProps.item(i).getFirstChild()
                        .getNodeValue());
                list.add(objectProps.item(i).getFirstChild().getNodeValue());

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
    }

    public String convertListToString() {

        StringBuilder sb = new StringBuilder();
        if (list.size() > 0) {
            sb.append(list.get(0));
            for (int i = 1; i < list.size(); i++) {
                sb.append(list.get(i));
            }
        }
        return sb.toString();
    }

}
public void ParserForObjectTypes() throws SAXException, IOException,
            ParserConfigurationException {

        try {
            FileInputStream file = new FileInputStream(new File(
                    "xmlFiles/CoreDatamodel.xml"));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory
                    .newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            XPath xPath = XPathFactory.newInstance().newXPath();
            NodeList nodeList = (NodeList) xPath.compile(
                    "//OBJECT_TYPE[text() = '" + obj.getObjectType()
                            + "']/following-sibling::prop[1]/*").evaluate(
                    xmlDocument, XPathConstants.NODESET);

            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i).getNodeName() + " = "
                        + nodeList.item(i).getTextContent());
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
    }
//OBJECT_TYPE[text() = 'SiteData']/following-sibling::prop[1]/*
String xpath = "//OBJECT_TYPE[text() = '" + getObjType() + "']/following-sibling::prop[1]/*"
XPath xPath =  XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList)xPath.compile("//OBJECT_TYPE[text() = '" + getObjType() + "']/following-sibling::prop[1]/*").evaluate(document, XPathConstants.NODESET);

for (int i = 0; i < nodeList.getLength(); i++)
{
  System.out.println(nodeList.item(i).getNodeName() + " = " + nodeList.item(i).getTextContent());
}
DESCRIPTION = Site parameters
PARENT = NULL
VIRTUAL = 0
VISIBLE = 1
PICTURE = NULL
HELP = 10008
MIN_NO = 1
MAX_NO = 1
NAME_FORMAT = NULL
/type/OBJECT_TYPE[. = 'some type']/following-sibling::prop[1]
XPath xp = XPathFactory.newInstance().newXPath();
final Map<String, Object> vars = new HashMap<String, Object>();
xp.setXPathVariableResolver(new XPathVariableResolver() {
  public Object resolveVariable(QName name) {
    return vars.get(name.getLocalPart());
  }
});

XPathExpression expr = xp.compile("/type/OBJECT_TYPE[. = $type]/following-sibling::prop[1]");

vars.put("type", "Data");
Node dataProps = (Node)expr.evaluate(doc, XPathConstants.NODE);

vars.put("type", "SiteData");
Node siteProps = (Node)expr.evaluate(doc, XPathConstants.NODE);

// taking the value from a variable
vars.put("type", obj.getObjectType());
Node objectProps = (Node)expr.evaluate(doc, XPathConstants.NODE);