简体   繁体   English

使用Java从XML文件读取功能值

[英]Read functioning value from XML file using java

I have 2 XML file . 我有2个XML文件。 In one XML file is the form which contains x and y coordinate values . 在一个XML文件中是包含x和y坐标值的表单。 In second XML file contains all the functioning/operation related to first XML which gives an output as an graph image . 在第二个XML文件中,包含与第一个XML有关的所有功能/操作,第一个XML提供输出为图形图像。

I need to know how can I read the function XML file specially for functioning in java program on button click ? 我需要知道如何在单击按钮时专门读取Java程序中的XML函数文件? I know to parse , but just normal parsing will work for the operation. 我知道要解析,但是只有正常的解析才能对该操作起作用。 What libraries do I have to pass for this ? 为此,我必须通过哪些库?

The functioning part is already there in XML . 功能部分已经存在于XML中。 I just need to call it to show output. 我只需要调用它即可显示输出。

part of form XML which will be used to save x and y coordinate:- XML格式的一部分,将用于保存x和y坐标:-

<cont format="%9.2f" hotlink="true" name="cont_2" refvar="gud/_ZSFR[29]" type="float" width="20" xpos="545" ypos="454">
            <PROPERTY min="0"/>
            <PROPERTY max="5000"/>
        </cont>

part of function xml file which will actually do operation of generating image by passing those form xml x and y values:- 功能xml文件的一部分,该文件实际上将通过传递xml x和y 形式的值来执行生成图像的操作:

<function name="string.length" return="filenamelength">file_name</function>
    <IF>
        <CONDITION>filenamelength>=1</CONDITION>
        <THEN>
            <op>new1content=new1content+_T"\\nR250="+_F%.3f"gud/_ZSFR[29]"</op>
            <op>new1content=new1content+_T"\\nR251="+_F%.3f"gud/_ZSFR[28]"</op>
<function name="doc.exist" return="mpf1ok">_T"\mpf\1.mpf" </function>

There are two kinds of Libraries in java for parsing XML files. java中有两种用于解析XML文件的库。 Streaming based and Tree based. 基于流和基于树。 If you just need to parse XML (Not concerned with generating XML) you can use Streaming API's are memory efficient. 如果您只需要解析XML(不关心生成XML),则可以使用Streaming API节省内存。 Stax is recommended in your case. 建议您使用Stax

It sounds like what you need is a combination of: 听起来您需要的是以下各项的组合:

  • javax.xml.parsers ... javax.xml.parsers ...
  • org.w3c.dom. ... ...

there is a good tutorial on extracting data from XML here at tutorialspoint their basic example shows the use of a method called Document.getElementByTagName(String tagName) which returns an Element object. 那里有一个很好的教程,关于如何从XML提取数据, 这里的教程指出了它们的基本示例,该示例显示了使用称为Document.getElementByTagName(String tagName)的方法的方法,该方法返回Element对象。 depending on how specific your element is you can also check attributes using: 根据元素的具体程度,您还可以使用以下方法检查属性:

  • Element.getAttribute(String attrName)
  • Element.hasAttribute(String attrName)

this should allow you to pinpoint the tag that you need after which you can use the methods shown in the tutorial to extract the content of the tags. 这应该使您可以精确定位所需的标签,然后可以使用教程中显示的方法提取标签的内容。

Another simple XML tutorial in Java Java中的另一个简单XML教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM