如何转换具有" ref ids"的XML文件和" ids"在java中

时间:2014-11-06 09:38:05

标签: java xml

我有一个类似下面的示例xml。我有一个要求,我需要交叉引用各种xml属性。我已经确定了以下格式,但不确定如何在java中解释这个。

我如何在java中解释这个xml。例如,如何使用教师ID =“T72100”

退出教师的详细信息
<schedule term="Fall" year="98" xmlns:data="x-schema:idSchema.xml"
                                xmlns:ref="x-schema:refSchema.xml">
  <classes>
    <data:class id="ENGL6004">
      <title>From Here to Eternity: Studies in the Futureenter code here
        and other Temporal Genres</title>
      <ref:teacherRef ref="T31330"/>
      <students>
        <ref:student ref="S50245"/>
        <ref:student ref="S87901"/>
        <ref:student ref="S19272"/>
        <ref:student ref="S48984"/>
      </students>
    </data:class>
    <data:class id="HIST6010">
      <title>The You Decade: A History of Finger Pointing
        in Post-War America</title>
      <ref:teacher ref="T72100"/>
      <students>
        <ref:student ref="S60912"/>
        <ref:student ref="S87901"/>
        <ref:student ref="S84281"/>
        <ref:student ref="S44098"/>
      </students>
    </data:class>
    <data:class id="ENGL6020">
      <title>Reading between the Lines: The Literature
        of Waiting</title>
      <ref:teacher ref="T31330"/>
      <students>
        <ref:student ref="S84281"/>
        <ref:student ref="S19272"/>
        <ref:student ref="S48984"/>
        <ref:student ref="S44098"/>
      </students>
    </data:class>
  </classes>
  <teachers>
    <data:teacher id="T31330">
      <name>Margaret Doornan</name>
      <position>Associate Professor</position>
      <classes>
        <ref:class ref="ENGL6004"/>
        <ref:class ref="ENGL6020"/>
      </classes>
    </data:teacher>
    <data:teacher id="T72100">
      <name>Hal Canter</name>
      <position>Instructor</position>
      <classes>
        <ref:class ref="HIST6010"/>
      </classes>
    </data:teacher>
  </teachers>
  <students>
    <data:student id="S44098">
      <name>Kelly Griftman</name>
      <year>Senior</year>
      <status>full-time</status>
      <classes>
        <ref:class ref="HIST6010"/>
        <ref:class ref="ENGL6020"/>
      </classes>
    </data:student>
    <data:student id="S48984">
      <name>Norbert James</name>
      <year>Senior</year>
      <status>full-time</status>
      <classes>
        <ref:class ref="ENGL6004"/>
        <ref:class ref="ENGL6020"/>
      </classes>
    </data:student>
    </students>
</schedule>

更新:尝试使用XPath来获取所需的输出但是我的表达式可能不正确或者我不太了解Xpath:

String expression = "/schedule/teachers/teacher[@id='T72100']";
        Node node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
        if(null != node) {
            NodeList nodeList = node.getChildNodes();
            for (int i = 0;null!=nodeList && i < nodeList.getLength(); i++) {
                Node nod = nodeList.item(i);
                if(nod.getNodeType() == Node.ELEMENT_NODE)
                    System.out.println(nodeList.item(i).getNodeName() + " : " + nod.getFirstChild().getNodeValue());
            }
        }

输出:

name : Hal Canter
position : Instructor
classes :

这不提供有关类的信息。如何检索类的详细信息。 预期的输出是

name : Hal Canter
    position : Instructor
    classes :
        id: HIST6010
        title: "The You Decade: A History of Finger Pointing in Post-War America"

1 个答案:

答案 0 :(得分:1)

不幸的是,XPath没有在文档中使用名称空间前缀definend。您需要xpath.setNamespaceContext(...)带前缀,或使用a library自动处理所有这些内容。

以下是将老师作为XMLBeam完成的对象阅读。阅读类元素是完全相同的。

public class TestXMLParsing {

public interface Schedule {

    public interface Teacher {
        @XBRead("./classes/ref:class/@ref")
        String[] getClassRefs();
        @XBRead("./name")
        String getName();
        @XBRead("./position")
        String getPosition();
    }

    @XBRead("//data:teacher[@id='{0}']")
    Teacher getTeacher(String id);
}

@Test
public void readTeacherAndClassesRef() throws IOException {
    Schedule schedule = new XBProjector(Flags.TO_STRING_RENDERS_XML).io().url("resource://schedule.xml").read(Schedule.class);
    Teacher teacher = schedule.getTeacher("T72100");
    System.out.println(teacher.getName()+"\n"+teacher.getPosition()+"\nclasses:");
    for (String classRef : teacher.getClassRefs()) {
        System.out.println(classRef);
    }
}

}

在我的眼里,这看起来更直接,也可以理解为手工处理DOM。