如何通过JDOM获取属性值

时间:2012-11-17 06:45:19

标签: xml-parsing jdom-2

我想从这个XML文件中获取元素“UML:UseCase”的属性“name”。

<?xml version = "1.0" encoding = "UTF-8"?>
<XMI xmi.version = "1.1" xmlns:UML="href://org.omg/UML/1.3" timestamp = "Fri Nov 16 11:47:6 2012">
<XMI.header>
  <XMI.documentation>
    <XMI.owner></XMI.owner>
    <XMI.contact></XMI.contact>
    <XMI.exporter>StarUML.XMI-Addin</XMI.exporter>
    <XMI.exporterVersion>1.0</XMI.exporterVersion>
    <XMI.notice></XMI.notice>
  </XMI.documentation>
  <XMI.metamodel xmi.name = "UML" xmi.version = "1.3"/>
 </XMI.header>
<XMI.content>
<UML:Model xmi.id="UMLProject.1">
  <UML:Namespace.ownedElement>
    <UML:Model xmi.id="UMLModel.2" name="Use Case Model" visibility="public" isSpecification="false" namespace="UMLProject.1" isRoot="false" isLeaf="false" isAbstract="false">
      <UML:Namespace.ownedElement>
        <UML:Package xmi.id="UMLPackage.3" name="Purchasing" visibility="public" isSpecification="false" namespace="UMLModel.2" isRoot="false" isLeaf="false" isAbstract="false">
          <UML:Namespace.ownedElement>
            <UML:UseCase xmi.id="UMLUseCase.4" name="Place order" visibility="public" isSpecification="false" namespace="UMLPackage.3" isRoot="false" isLeaf="false" isAbstract="false" participant="UMLAssociationEnd.40" extend2="UMLExtend.47 UMLExtend.48" include="UMLInclude.25 UMLInclude.44 UMLInclude.45"/>
            <UML:UseCase xmi.id="UMLUseCase.5" name="Place order via Web site" visibility="public" isSpecification="false" namespace="UMLPackage.3" isRoot="false" isLeaf="false" isAbstract="false" extend="UMLExtend.48"/>
            <UML:UseCase xmi.id="UMLUseCase.6" name="Place order via Application" visibility="public" isSpecification="false" namespace="UMLPackage.3" isRoot="false" isLeaf="false" isAbstract="false" extend="UMLExtend.47"/>
            <UML:UseCase xmi.id="UMLUseCase.7" name="Browse catalog" visibility="public" isSpecification="false" namespace="UMLPackage.3" isRoot="false" isLeaf="false" isAbstract="false" participant="UMLAssociationEnd.43"/>
            <UML:UseCase xmi.id="UMLUseCase.8" name="Confirm shipment" visibility="public" isSpecification="false" namespace="UMLPackage.3" isRoot="false" isLeaf="false" isAbstract="false"/>
            <UML:Actor xmi.id="UMLActor.9" name="Customer" visibility="public" isSpecification="false" namespace="UMLPackage.3" isRoot="false" isLeaf="false" isAbstract="false" participant="UMLAssociationEnd.39 UMLAssociationEnd.42"/>
            <UML:Stereotype xmi.id="X.55" name="Common feature" extendedElement="UMLPackage.3"/>
            <UML:Stereotype xmi.id="X.56" name="Common use case" extendedElement="UMLUseCase.4 UMLUseCase.7 UMLUseCase.8"/>
            <UML:Stereotype xmi.id="X.57" name="Alternative use case" extendedElement="UMLUseCase.5 UMLUseCase.6"/>
          </UML:Namespace.ownedElement>
        </UML:Package>
......

我的预期结果。

下订单
通过网站下订单
通过申请下订单 浏览目录
确认发货

谢谢

1 个答案:

答案 0 :(得分:0)

为您编写程序需要付出太多努力...但您需要使用命名空间来调查XPath:

XPathFactory.compile("//UML:UseCase/@name", Filters.attribute(), null,
         Namespace.getNamespace("UML", "href://org.omg/UML/1.3"));

或直接使用命名空间导航:

Parent.getChildren(name, Namespace).

您的问题是您需要使用名称空间Namespace.getNamespace(“UML”,“href://org.omg/UML/1.3”)访问所有带有UML前缀的内容;

罗尔夫