简体   繁体   中英

How to modify the attribute values in XML file using Java

I need to modifies the XML, I have a parent tag parts which contain child tag part and Child tag sub child tags, They are Item, brand, Manufacturer, Model, Cost . And it have a Brands tag and it contain Tag name called Brand . I need to change the brand attribute (identifier) value. If the Brand child tag Contain (textcontent) text (Geforce_GT) is equal to the text in part tag : brand child tag : text (Geforce_GT) . Then Id attribute value of brand tag in part tag should assign to the Identifeir attribute in Brand tag child of Brands Parent tag .

<?xml version="1.0" ?>
<!DOCTYPE PARTS SYSTEM "parts.dtd">
<?xml-stylesheet type="text/css" href="xmlpartsstyle.css" ?>
<PARTS>
  <TITLE>Computer Parts</TITLE>
  <PART>
    <ITEM id="CP1809_E1">Motherboard</ITEM>
    <MANUFACTURER>ASUS</MANUFACTURER>
    <MODEL>P3B-F</MODEL>
    <COST>123.00</COST>
  </PART>
  <PART>
    <ITEM id="CP1809_E2">Video Card</ITEM>
    <BRAND id="CP1809_B1">Geforce_GT</BRAND>
    <MANUFACTURER>ATI</MANUFACTURER>
    <MODEL>All-in-Wonder Pro</MODEL>
    <BRAND id="CP1809_B2">730_64-BIT</BRAND>
    <COST>160.00</COST>
  </PART>
  <PART>
    <ITEM id="CP1809_E3">Sound Card</ITEM>
    <MANUFACTURER>Creative Labs</MANUFACTURER>
    <MODEL>Sound Blaster Live</MODEL>
    <COST>80.00</COST>
  </PART>
  <PART>
    <ITEM id="CP1809_E3">inch Monitor</ITEM>
    <MANUFACTURER>LG Electronics</MANUFACTURER>
    <MODEL>995E</MODEL>
    <COST>290.00</COST>
  </PART>
  <BRANDS>
    <BRAND identifier="CP1809_E2">
      <TEXTCONTENT>Geforce_GT</TEXTCONTENT>
    </BRAND>
    <BRAND identifier="CP1809_E2">
      <TEXTOVERVIEW>730_64-BIT</TEXTOVERVIEW>
    </BRAND>
    <BRAND identifier="B1809_E3">
      <TEXT>Empty</TEXT>
    </BRAND>
    <BRAND identifier="B1809_E4">
      <TEXT>Empty</TEXT>
    </BRAND>
  </BRANDS>
</PARTS>

I need to make modifications such as:

<BRANDS>
  <BRAND identifier = "CP1809_B1">
    <TEXTCONTENT>Geforce_GT<TEXTCONTENT>
  </BRAND>
  <BRAND identifier = "CP1809_B2">
    <TEXTCONTENT>730_64-BIT<TEXTCONTENT>
  </BRAND>
  <!-- ... -->
<BRANDS>

XSLT can do that (see example at http://xsltransform.net/ej9EGda ):

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:key name="desc" match="PART/BRAND" use="."/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="BRANDS/BRAND[key('desc', TEXTCONTENT)]/@identifier">
        <xsl:attribute name="{name()}">
            <xsl:value-of select="key('desc', ../TEXTCONTENT)/@id"/>
        </xsl:attribute>
    </xsl:template>

</xsl:transform>

On the Java platform you have a choice of XSLT processors like Saxon 9 for XSLT 2.0 or Xalan or Saxon 6 for XSLT 1.0 and the built-in version of Xalan in the JRE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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