我想使用xpath java读取xml,读取父节点并获得其计数,之后我必须使用父节点并获得子节点计数
父节点是-

请有人帮助我。



<?xml version="1.0" encoding="UTF-8"?>

-<ab_rsp status="ok">


-<itm title="United States" guid="04570A9A-6066-11E2-BB55-FF519035903F" type="country">


-<itm title="Arizona" guid="A83274AC-901A-11DF-A622-0C319DFF4B22" type="authority">


-<itm title="Academic Standards" guid="9935C580-C0DA-11DA-80AA-DCC515614119" type="document">


-<itm title="Technology" guid="E3E0AD06-FC32-11D9-8407-9AE6FB2C8371" type="subject">

<meta name="year">2000</meta>


-<itm title="Readiness (Kindergarten)" guid="001DCFCC-2A74-11D8-A1A9-EB97E8AED672" type="grade">

<meta name="grade" lo="K" hi="K"/>


-<itm guid="929A5FA4-29E4-11D8-9FDA-DEE83CF00340" type="standard">

<meta name="num">1T-R</meta>

<meta name="descr">Fundamental Operations and Concepts</meta>


-<itm guid="92A019DA-29E4-11D8-94F3-918A2ADD362B" type="standard">

<meta name="num">1T-R1</meta>

<meta name="descr">Communicate about basic technology components using developmentally appropriate and accurate terminology</meta>


-<itm guid="92A2E14C-29E4-11D8-9510-E88CF3390259" type="standard">

<meta name="num">1T-R1.PO 1</meta>

<meta name="descr">Use basic vocabulary related to the use of technology (e.g., mouse, keyboard, monitor, toolbar, menu, window, folder, icon, spreadsheet, word processor, cassette player, CD player versus DVD versus video tape, video camera)</meta>

</itm>


-<itm guid="92A5BFAC-29E4-11D8-B459-9A03B60B3918" type="standard">

<meta name="num">1T-R1.PO 2</meta>

<meta name="descr">Identify the components of a computer (e.g., mouse, keyboard, monitor, CPU, printer)</meta>

</itm>

</itm>


-<itm guid="92A89C36-29E4-11D8-80CB-9286DFCA4E08" type="standard">

<meta name="num">1T-R2</meta>

<meta name="descr">Use input devices and output devices successfully to operate computers, VCRs, audiotapes, and other technologies</meta>


-<itm guid="92AB7870-29E4-11D8-81C8-FBD15B9102AD" type="standard">

<meta name="num">1T-R2.PO 1</meta>

<meta name="descr">Demonstrate start up and shut down procedures of basic technology components (e.g., computers, tape recorders, cassette players, VCRs)</meta>

</itm>


-<itm guid="92AE5900-29E4-11D8-BADC-9BE3FF051872" type="standard">

<meta name="num">1T-R2.PO 2</meta>

<meta name="descr">Use devices to complete a task (e.g., mouse, keyboard, printer, remote control, microphone)</meta>

</itm>

</itm>

</itm>


-<itm guid="92B11F50-29E4-11D8-8666-D70924C27789" type="standard">

<meta name="num">2T-R</meta>

<meta name="descr">Social, Ethical and Human Issues</meta>


-<itm guid="92B9B4EE-29E4-11D8-8819-C3322442EC44" type="standard">

<meta name="num">2T-R1</meta>

<meta name="descr">Work cooperatively and collaboratively when using technology in the classroom</meta>


-<itm guid="92BCA960-29E4-11D8-A217-F8D5963215AE" type="standard">

<meta name="num">2T-R1.PO 1</meta>

<meta name="descr">Demonstrate respect for other students while using technology (e.g., take turns, share resources)</meta>

</itm>


-<itm guid="92BF7104-29E4-11D8-9926-9EF61CBC1B4F" type="standard">

<meta name="num">2T-R1.PO 2</meta>

<meta name="descr">Demonstrate appropriate behavior (e.g., use only your documents and folders)</meta>

</itm>

</itm>


-<itm guid="92C24E38-29E4-11D8-91F7-D0E3C7F2A956" type="standard">

<meta name="num">2T-R2</meta>

<meta name="descr">Practice responsible use of technological devices</meta>


-<itm guid="92C52BD0-29E4-11D8-9F50-8FC6DBAC1404" type="standard">

<meta name="num">2T-R2.PO 1</meta>

<meta name="descr">Operate equipment to ensure equipment is unharmed (e.g., do not bang on keys; no food or objects near equipment; care for disks and CD-ROM; use proper shut down procedures)</meta>

</itm>


-<itm guid="92C8072E-29E4-11D8-A2CE-8EBA3106B953" type="standard">

<meta name="num">2T-R2.PO 2</meta>

<meta name="descr">Recognize that damaging school equipment is destroying public property</meta>

</itm>


-<itm guid="92CAE4DA-29E4-11D8-8F74-80A2BA5F84A1" type="standard">

<meta name="num">2T-R2.PO 3</meta>

<meta name="descr">Recognize that changing someone’s work without permission is unacceptable</meta>

</itm>

</itm>

</itm>


-<itm guid="92EA3CCC-29E4-11D8-A09B-DF874AC58F3F" type="standard">

<meta name="num">3T-R</meta>

<meta name="descr">Technology Productivity Tools</meta>


-<itm guid="92CDC29A-29E4-11D8-9E76-C7CA8D85B9B7" type="standard">

<meta name="num">3T-R1</meta>

<meta name="descr">Use technology drawing tools for communicating and illustrating</meta>


-<itm guid="92D92248-29E4-11D8-B2AD-9AD0B7A1AB7B" type="standard">

<meta name="num">3T-R1.PO 1</meta>

<meta name="descr">Using a drawing program, create a picture story with support from teacher, family members or student partners</meta>

</itm>


-<itm guid="92D644BA-29E4-11D8-8BF2-D14836F4F6A1" type="standard">

<meta name="num">3T-R1.PO 2</meta>

<meta name="descr">Using a drawing program, add name and letters to illustrations</meta>

</itm>

</itm>

</itm>

</itm>

</itm>

</itm>

</itm>

</itm>

</ab_rsp>





提前致谢

最佳答案

您需要阅读xpath文档-有很多方法

你可以用它来获取你的节点

XPath xPath = XPathFactory.newInstance().newXPath();
String expression="//itm";

NodeList nodes  = (NodeList)  xPath.compile(expression).evaluate(document, XPathConstants.NODESET);


这是为了得到他们的父母

nodep = node.getParentNode();


一个孩子

 the_node.getFirstChild();


这是为了让他们的孩子(如果您有一个元素)

if  (the_node instanceof Element)
{
  Element the_element=(Element) the_node;

  NodeList nodes  = the_element.getChildNodes();


数是直接的

nodes.getLength()


要迭代,它也是直接的

for(int i=0; i<nodes.getLength(); i++)
{
 Node the_node = nodes.item(i);


希望这可以帮助

08-29 00:07