Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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读取XML的父属性值_Java - Fatal编程技术网

如果子属性值匹配,如何通过Java读取XML的父属性值

如果子属性值匹配,如何通过Java读取XML的父属性值,java,Java,这里是我的XML文件,我的XML文件大小超过100 MB <?xml version="1.0" encoding="UTF-8"?> <products inbound.code.version="" schema.version="" xslt.version="0" hierarchy.type="provisioner" hierarchy.remap="emea" hierarchy.language="en" hierarchy.country="eu" h

这里是我的XML文件,我的XML文件大小超过100 MB

<?xml version="1.0" encoding="UTF-8"?>
    <products inbound.code.version="" schema.version="" xslt.version="0" hierarchy.type="provisioner" hierarchy.remap="emea" hierarchy.language="en" hierarchy.country="eu" hierarchy.translation="en" hierarchy.depth="product" remap="product" depth="0">
    <product.type name="myprod" translation="myprod" change="modified" oid="84609819" oid.2="84609819" common.oid="3709945" remap="category" depth="1">
    <category name="myprod Adapters" translation="myprod Adapters" change="modified" oid="84609824" oid.2="84609824" common.oid="3710111" remap="category" depth="2">
    <sub.category name="myprod Ethernet Adapters" translation="myprod Ethernet Adapters" change="modified" oid="81995406" oid.2="81995406" common.oid="1844132" remap="category" depth="3">
    <big.series name="Nova myprod Ethernet Adapters" translation="Nova myprod Ethernet Adapters" change="modified" oid="81996409" oid.2="81996409" common.oid="496741" remap="category" depth="4">
    <small.series name="Nova myprod Dual NC370i Multifunction Network Adapter" translation="Nova myprod Dual NC370i Multifunction Network Adapter" change="modified" oid="81997928" oid.2="81997928" common.oid="496742" remap="category" depth="5">
    <model name="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" translation="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" change="modified" oid="82020916" oid.2="82020916" common.oid="496746" remap="category" depth="6">
    <product name="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" translation="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" number="product1" change="modified" oid="64857725" oid.2="64857725" common.oid="496743" remap="product" depth="7"/>
    </model>
    <model name="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" translation="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" change="modified" oid="82020916" oid.2="82020916" common.oid="496746" remap="category" depth="6">
    <product name="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" translation="Nova BL20p G3 Dual NC370i Multifunction Network Adapter" number="product2" change="modified" oid="64857725" oid.2="64857725" common.oid="496743" remap="product" depth="7"/>
    </model>
         </small.series>
        </big.series>
       </sub.category>
      </category>
     </product.type>
    </Nova.products>

我必须通过Java输入一个产品编号作为搜索参数,如果该产品与通过Java输入的number=“product number”匹配
in depth=“7”然后我必须从depth=“6”获取common.oid属性值。

尝试使用sax解析器解析此xml文档(建议只读)

重写startElement(…)-方法:

  • 如果起始元素是候选元素,则将其存储在变量中
  • 如果start元素符合您的需要,您可以参考父元素
重写endElement(..)-方法

  • 如果(结束元素)是候选元素(类似于startElement),则将候选元素设置为null
此代码段可能有助于:

private Object candidate; //you must define your own kind of candidate!
public void findIt(){ //stupid name, you can do better
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();

        DefaultHandler handler = new DefaultHandler() {

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

                if (qName.equalsIgnoreCase(candidateTagName) ){
                     candidate = new Object();
                     //populate the candidate with required infos
                     //use  attributes.getValue(attributeName).trim();
                     //this is your parent on level n
                }   

                if (qName.equalsIgnoreCase(childTagName) ){
                     //now you found your child at level n+1
                     //and you can refer to parent at level n:
                     candidate.getSomething();
                     //or do something
                     //or even return something (requires method return value change)
                }   
            }

            @Override
            public void endElement(String arg0, String arg1, String arg2) throws SAXException {
                super.endElement(arg0, arg1, arg2);

                if (qName.equalsIgnoreCase(candidateTagName) ){
                    candidate = null;
                }

             }
        }
    };

    saxParser.parse(xmlFile, handler);

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
你的候选对象必须被定义,你可以比我做得更好! 并定义您的标签